UITable Viewのスクラップ削除の実現


回転:http://rainbird.blog.51cto.com/211214/634587
前の準備:
第一歩は、データソースを準備します.
 

  
  
  
  
  1. #import <UIKit/UIKit.h> 
  2.   
  3. @interface UITableCellSwapDeleteViewController : UIViewController <UITableViewDelegate>{ 
  4.     IBOutlet UITableView *testTableView; 
  5.     NSMutableArray *dataArray; 
  6. @property (nonatomic, retain) UITableView *testTableView; 
  7. @property (nonatomic, retain) NSMutableArray *dataArray; 
  8. @end 
  9.   
  10. - (void)viewDidLoad { 
  11.     [super viewDidLoad]; 
  12.     dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil]; 
  13.      
ここで筆者は一次元の可変配列を定義して実現しました.なぜ可変配列を使うのですか?中のデータを削除しますから.
 
第二のステップは、データを示す.
 

  
  
  
  
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
  2.     // Return the number of sections. 
  3.     return 1; 
  4.   
  5.   
  6. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
  7.     // Return the number of rows in the section. 
  8.     return [dataArray count]; 
  9.   
  10.   
  11. // Customize the appearance of table view cells. 
  12. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
  13.      
  14.     static NSString *CellIdentifier = @"Cell"
  15.      
  16.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
  17.     if (cell == nil) { 
  18.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
  19.     } 
  20.      
  21.     // Configure the cell... 
  22.     cell.textLabel.text = [dataArray objectAtIndex:indexPath.row]; 
  23.     return cell; 
上記の3つのプロキシ方法を実現することにより、UITable Viewにデータを追加した.
UITableView划动删除的实现_第1张图片
 
上の2ステップを通じて、データの展示作業を実現しました.次はキーデータの削除を実現します.
 
 

  
  
  
  
  1. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
  2.     return YES; 
  3.   
  4. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
  5.   
  6.      if (editingStyle == UITableViewCellEditingStyleDelete) { 
  7.          [dataArray removeObjectAtIndex:indexPath.row]; 
  8.          // Delete the row from the data source. 
  9.          [testTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
  10.           
  11.      }    
  12.      else if (editingStyle == UITableViewCellEditingStyleInsert) { 
  13.      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
  14.      }    
  15.  } 
 上の2つのエージェントを有効にして、データの削除操作を追加します.
[dataAray removeObject AtIndex:indexPath.row]
 一つのデータを右に動かしてください.
UITableView划动删除的实现_第2张图片
 ポイントDelete.
UITableView划动删除的实现_第3张图片
 データを削除しましたか?
 話はここまでで終わります.でも、拡張したいです.図を見てください.後の「Delete」に注意してください.このものを変えたい衝動がありますか?ダウンロードしますか実は簡単です.実はこの代理方法は以下の通りです.
 

  
  
  
  
  1. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ 
  2.         return @" "
もう一回漕ぎます.変わりましたか?
UITableView划动删除的实现_第4张图片