UITableViewCell的標記、移動、刪除、插入

 這篇文章是建立在  

代碼實現 UITableView與UITableViewCell基礎上進行修改,用不上的代碼我註釋調,部分不明白可以看看上篇博客;實現的功能是對UITableViewCell的標記、移動、刪除、插入;


1.標記:指的是選中某一行,在這一行後面有個符號,常見的是對勾形式
通過修改cell的accessoryType屬性來實現,首先,在ViewDidLoad中[tableView setEditing:NO animated:YES];表示把單元格可編輯狀態這隻爲NO
  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];  
  4.     if (cellView.accessoryType == UITableViewCellAccessoryNone) {  
  5.         cellView.accessoryType=UITableViewCellAccessoryCheckmark;  
  6.     }  
  7.     else {  
  8.         cellView.accessoryType = UITableViewCellAccessoryNone;  
  9.         [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  10.     }  
  11. }  

 當我們選中單元格的時候,調用此函數,首先是indexPath檢測選中了哪一行,if判斷當前單元格是否被標記,也就是當前單元格風格,是否爲UITableViewCellAccessoryCheckmark風格,如果是,則換成UITableViewCellAccessoryNone(不被標記風格)風格,以下accessoryType四個風格屬性
 UITableViewCellAccessoryCheckmark                 UITableViewCellAccessoryDetailDisclosureButton
        

UITableViewCellAccessoryDisclosureIndicator   UITableViewCellAccessoryNone
     

2.移動  
實現移動單元格就需要把單元格的編輯屬性設置爲YES,[tableView setEditing:YES animated:YES];
  1. //返回YES,表示支持單元格的移動  
  2. -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     return YES;  
  5. }  
  1. //單元格返回的編輯風格,包括刪除 添加 和 默認  和不可編輯三種風格  
  2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     return UITableViewCellEditingStyleInsert;  
  5. }  
三種風格的分別是

UITableViewCellEditingStyleDelete                                                UITableViewCellEditingStyleInsert

  


UITableViewCellEditingStyleNone


實現移動的方法
  1. -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  
  2. {  
  3. //    需要的移動行  
  4.     NSInteger fromRow = [sourceIndexPath row];  
  5. //    獲取移動某處的位置  
  6.     NSInteger toRow = [destinationIndexPath row];  
  7. //    從數組中讀取需要移動行的數據  
  8.     id object = [self.listData objectAtIndex:fromRow];  
  9. //    在數組中移動需要移動的行的數據  
  10.     [self.listData removeObjectAtIndex:fromRow];  
  11. //    把需要移動的單元格數據在數組中,移動到想要移動的數據前面  
  12.     [self.listData insertObject:object atIndex:toRow];    
  13. }  

單元格的移動是選中單元格行後面三條橫線纔可以實現移動的

  
3.刪除
首先是判斷(UITableViewCellEditingStyle)editingStyle,所以
  1. //單元格返回的編輯風格,包括刪除 添加 和 默認  和不可編輯三種風格  
  2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     return UITableViewCellEditingStyleDelete;  
  5. }  

  1. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     if (editingStyle==UITableViewCellEditingStyleDelete) {  
  4. //        獲取選中刪除行索引值  
  5.         NSInteger row = [indexPath row];  
  6. //        通過獲取的索引值刪除數組中的值  
  7.         [self.listData removeObjectAtIndex:row];  
  8. //        刪除單元格的某一行時,在用動畫效果實現刪除過程  
  9.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
  10.     }  
  11. }  

刪除了張四 效果圖:
    
4.添加
實現方法和刪除方法相同,首先還是返回單元格編輯風格
  1. //單元格返回的編輯風格,包括刪除 添加 和 默認  和不可編輯三種風格  
  2. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  3. {  
  4.     return UITableViewCellEditingStyleInsert;  
  5. }  

爲了顯示效果明顯,在.h文件中聲明一個變量i
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>  
  4. {  
  5.  NSInteger i;  
  6. }  
  7. @property(strong,nonatomic) NSMutableArray *listData;  
  8. @property(strong,nonatomic)UITableView *tableView;  
  9. @property(strong,nonatomic)UITableViewCell *tableViewCell;  
  10.   
  11. @end  

  1. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     if (editingStyle==UITableViewCellEditingStyleDelete) {  
  4. //        獲取選中刪除行索引值  
  5.         NSInteger row = [indexPath row];  
  6. //        通過獲取的索引值刪除數組中的值  
  7.         [self.listData removeObjectAtIndex:row];  
  8. //        刪除單元格的某一行時,在用動畫效果實現刪除過程  
  9.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];  
  10.     }  
  11.     if(editingStyle==UITableViewCellEditingStyleInsert)  
  12.     {  
  13.          
  14.         i=i+1;  
  15.         NSInteger row = [indexPath row];  
  16.         NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];  
  17.         NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];  
  18. //        添加單元行的設置的標題  
  19.         [self.listData insertObject:mes atIndex:row];  
  20.         [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];  
  21.     }  
  22. }  

運行效果圖:
     

在刪除和添加單元格的用到UITableViewRowAnimation動畫效果,它還有其他幾種效果,在此不做測試

UITableViewRowAnimationAutomatic      UITableViewRowAnimationTop 

UITableViewRowAnimationBottom          UITableViewRowAnimationLeft

UITableViewRowAnimationRight             UITableViewRowAnimationMiddle

UITableViewRowAnimationFade              UITableViewRowAnimationNone




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章