UITableView划動刪除的實現

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。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; 
通過實現上面三個代理方法向UITableView中添加了數據.

圖1

 
通過上面兩步就實現了數據展示工作,接下就實現關鍵的數據刪除了.
 
 
  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.  } 
 啓用上面兩個代理,並增加數據刪除操作:
[dataArray removeObjectAtIndex:indexPath.row];
 在一條數據上向右划動一下.

 點Delete.

 是不是就成功刪除了一條數據呢?
 按理說故事講到這裏也就講完了.但是筆者想延伸一下.注意看圖二劃動以後的"Delete",你有沒有想把這個東東改掉的衝動呢?比如改成:下載?其實很簡單,其實下面這個代理方法:
 
  1. - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ 
  2.         return @"下載"
再划動一下,是不是變了呢?

 

相關文章:

UITableView多選刪除,類似mail中的多選刪除效果

發佈了61 篇原創文章 · 獲贊 60 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章