IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
2014-04-17 14:43
911 查看
表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的
下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码。
显示下过如下:
下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码。
显示下过如下:
#pragma mark #pragma mark Table View data source //setEditing:animated:后被调用 //询问具体Cell是不是支持编辑 -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { NSMutableArray *arrNewDatasource=[NSMutableArray arrayWithArray:self.datasource]; //cell新增处理 if(editingStyle == UITableViewCellEditingStyleInsert) { //新建一个HBPlayer对象 HBPlayerInfo *newPlayer=[[HBPlayerInfo alloc]init]; newPlayer.name=@"My Player"; newPlayer.role=@"GoalKeeper"; newPlayer.number=@"19"; //插入 [arrNewDatasource insertObject:newPlayer atIndex:indexPath.row]; //更新datasource _datasource = [[NSArray alloc]initWithArray:arrNewDatasource]; //更新界面 [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"InfoTableViewCellId"; HBCustomCell *cell =(HBCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { NSArray *arrNib=[[NSBundle mainBundle]loadNibNamed:@"CustomView" owner:self options:nil]; if(arrNib) { //第一个元素就是需要的UITableViewCell cell=(HBCustomCell *)[arrNib objectAtIndex:0]; } } UIImage *photo = nil; HBPlayerInfo *onePlayer=[self.datasource objectAtIndex:indexPath.row]; if(onePlayer) { cell.playerName.text=onePlayer.name; cell.playerName.font=[UIFont fontWithName:@"Helvetica" size:16.0f]; cell.playerRole.text=onePlayer.role; cell.playerRole.font=[UIFont fontWithName:@"Helvetica" size:12.0f]; cell.playerNumber.text=[NSString stringWithFormat:@"No.%@",onePlayer.number]; //插入时,更新界面的方法insertRowsAtIndexPaths会调用cellForRowAtIndexPath询问具体的UITableViewCell //所以这里考虑为插入的元素准备的空头像 photo=[UIImage imageNamed:@"gaolin.jpeg"]; if(!photo) { photo=[UIImage imageNamed:@"empty"]; } cell.playerPhoto.image=photo; } return cell; } #pragma mark #pragma mark TableView Delegate -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleInsert; /* //只有编辑状态时,才有删除功能 //由于手指左划显示“Delete”按钮并不处于编辑状态,所以会被屏蔽掉 if(self.tableView.editing) { return UITableViewCellEditingStyleDelete; } return UITableViewCellEditingStyleNone; */ }
相关文章推荐
- IOS 表视图(UITableVIew)的使用方法(8)表视图的编辑功能(多选)
- IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
- IOS 表视图(UITableVIew)的使用方法(4)自定义表视图单元
- iOS 表视图(UITableVIew)的使用方法(1)表视图的示例
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
- IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示
- iOS UITableView的编辑方法
- ios--UITableview的基本使用方法
- iOS UITableView reloadData 视图漂移或者闪动解决方法
- IOS UITableViewUITableView小技巧--实现cell向左滑动删除,编辑等功能
- IOS中集合视图UICollectionView中DecorationView的简易使用方法
- iOS中UITableView使用总结和 iOS UITableView代理方法详解
- iOS UITableView reloadData 时视图漂移或者闪动解决方法
- UItableView编辑的使用edit, UItableView的一些常用方法
- [转]IOS 学习笔记(8) 滚动视图(UIScrollView)的使用方法
- 使用UItableview在iOS应用开发中实现好友列表功能
- iOS开发:UITableView的deleteRowsAtIndexPaths方法的使用
- IOS addChildViewController方法使用及注意事项
- 浅谈ios设计之使用表格UITableVIew设计通讯录的方法