IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2635字,纯文字阅读大概需要4分钟。
内容图文
![IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)](/upload/InfoBanner/zyjiaocheng/1306/85d6969b17e0418a9f8c5a9476034906.jpg)
表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的
下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码。
显示下过如下:
#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; */ }
原文:http://www.cnblogs.com/haibosoft/p/3670731.html
内容总结
以上是互联网集市为您收集整理的IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)全部内容,希望文章能够帮你解决IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。