代碼實現 UITableView與UITableViewCell基礎上進行修改,用不上的代碼我註釋調,部分不明白可以看看上篇博客;實現的功能是對UITableViewCell的標記、移動、刪除、插入;
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cellView = [tableView cellForRowAtIndexPath:indexPath];
- if (cellView.accessoryType == UITableViewCellAccessoryNone) {
- cellView.accessoryType=UITableViewCellAccessoryCheckmark;
- }
- else {
- cellView.accessoryType = UITableViewCellAccessoryNone;
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- }
當我們選中單元格的時候,調用此函數,首先是indexPath檢測選中了哪一行,if判斷當前單元格是否被標記,也就是當前單元格風格,是否爲UITableViewCellAccessoryCheckmark風格,如果是,則換成UITableViewCellAccessoryNone(不被標記風格)風格,以下是accessoryType四個風格屬性
- //返回YES,表示支持單元格的移動
- -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- //單元格返回的編輯風格,包括刪除 添加 和 默認 和不可編輯三種風格
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleInsert;
- }
UITableViewCellEditingStyleDelete UITableViewCellEditingStyleInsert
UITableViewCellEditingStyleNone
- -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
- {
- // 需要的移動行
- NSInteger fromRow = [sourceIndexPath row];
- // 獲取移動某處的位置
- NSInteger toRow = [destinationIndexPath row];
- // 從數組中讀取需要移動行的數據
- id object = [self.listData objectAtIndex:fromRow];
- // 在數組中移動需要移動的行的數據
- [self.listData removeObjectAtIndex:fromRow];
- // 把需要移動的單元格數據在數組中,移動到想要移動的數據前面
- [self.listData insertObject:object atIndex:toRow];
- }
- //單元格返回的編輯風格,包括刪除 添加 和 默認 和不可編輯三種風格
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleDelete;
- }
- -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle==UITableViewCellEditingStyleDelete) {
- // 獲取選中刪除行索引值
- NSInteger row = [indexPath row];
- // 通過獲取的索引值刪除數組中的值
- [self.listData removeObjectAtIndex:row];
- // 刪除單元格的某一行時,在用動畫效果實現刪除過程
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- }
刪除了張四 效果圖:
- //單元格返回的編輯風格,包括刪除 添加 和 默認 和不可編輯三種風格
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return UITableViewCellEditingStyleInsert;
- }
爲了顯示效果明顯,在.h文件中聲明一個變量i
- #import <UIKit/UIKit.h>
- @interface STViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
- {
- NSInteger i;
- }
- @property(strong,nonatomic) NSMutableArray *listData;
- @property(strong,nonatomic)UITableView *tableView;
- @property(strong,nonatomic)UITableViewCell *tableViewCell;
- @end
- -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (editingStyle==UITableViewCellEditingStyleDelete) {
- // 獲取選中刪除行索引值
- NSInteger row = [indexPath row];
- // 通過獲取的索引值刪除數組中的值
- [self.listData removeObjectAtIndex:row];
- // 刪除單元格的某一行時,在用動畫效果實現刪除過程
- [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
- }
- if(editingStyle==UITableViewCellEditingStyleInsert)
- {
- i=i+1;
- NSInteger row = [indexPath row];
- NSArray *insertIndexPath = [NSArray arrayWithObjects:indexPath, nil];
- NSString *mes = [NSString stringWithFormat:@"添加的第%d行",i];
- // 添加單元行的設置的標題
- [self.listData insertObject:mes atIndex:row];
- [tableView insertRowsAtIndexPaths:insertIndexPath withRowAnimation:UITableViewRowAnimationRight];
- }
- }
UITableViewRowAnimationAutomatic UITableViewRowAnimationTop
UITableViewRowAnimationBottom UITableViewRowAnimationLeft
UITableViewRowAnimationRight UITableViewRowAnimationMiddle
UITableViewRowAnimationFade UITableViewRowAnimationNone