iOS学习之UITableView中Cell…
来源:互联网 发布:阿里云地域可以更换吗 编辑:程序博客网 时间:2024/06/06 10:38
接着iOS学习之TableView的简单使用
要进行数据的操作了,把代码里的不可变数组改成可变的:
NSArray
1、标记Cell。
效果如下:
打开项目,
-(void)tableView:(UITableView
添加代码
- -(void)tableView:(UITableView
*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ - //
NSString *rowString = [self.list objectAtIndex:[indexPath row]]; - //
UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; - //
[alter show]; -
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; -
if (cell.accessoryType == UITableViewCellAccessory None) { -
cell.accessoryType = UITableViewCellAccessory Checkmark; -
}else { -
cell.accessoryType = UITableViewCellAccessory None; -
} -
[tableView deselectRowAtIndexPath:indexPath animated:YES]; - }
UITableViewCellAccessory
UITableViewCellAccessory
UITableViewCellAccessory
UITableViewCellAccessory
可以自己试试。
2、删除Cell
想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。
打开xib,生成Table的IBoutlet映射
在viewDidload里添加
这是启动运行程序,
打开可编辑模式,默认情况显示删除的图标的。
实现删除的代码:
- -
( void)tableView:(UITableView*)tableView commitEditingStyle: - (UITableViewCellEditingSt
yle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { -
NSUInteger row = [indexPath row]; -
if (editingStyle == UITableViewCellEditingSt yleDelete) { -
[self.list removeObjectAtIndex:row]; -
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] -
withRowAnimation:UITableViewRowAnimationA utomatic]; -
} - }
这个方法根据参数editingStyle是UITableViewCellEditingSt
在这删除行的方法又出现了一个常量:UITableViewRowAnimationA
UITableViewRowAnimationA
UITableViewRowAnimationT
UITableViewRowAnimationB
UITableViewRowAnimationL
UITableViewRowAnimationR
UITableViewRowAnimationM
UITableViewRowAnimationF
UITableViewRowAnimationN
从常量名称打开可以看出效果来。
这是运行,就可以删除其中的一个Cell行了。
3、移动Cell
添加代码如下:
3.1先把默认的删除的图标去掉
- -
(UITableViewCellEditingSt yle)tableView:(UITableView *)tableView -
editingStyleForRowAtInde xPath:(NSIndexPath *)indexPath { -
return UITableViewCellEditingSt yleInsert; - }
3.2返回当前Cell是否可以移动
- -
( BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { -
return YES; - }
- -
( void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath *) - sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath { -
NSUInteger fromRow = [sourceIndexPath row]; -
NSUInteger toRow = [destinationIndexPath row]; -
-
id object = [self.list objectAtIndex:fromRow]; -
[self.list removeObjectAtIndex:fromRow]; -
[self.list insertObject:object atIndex:toRow]; - }
怎么移动呢?不要以为按住行的任何地方都能移动,要按住最左边的三道杠的图标才能拖动移动
4、插入cell:
4.1插入和删除差不多,在
- (void)tableView:(UITableView
(UITableViewCellEditingSt
添加UITableViewCellEditingSt
- -
( void)tableView:(UITableView*)tableView commitEditingStyle: - (UITableViewCellEditingSt
yle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { -
NSUInteger row = [indexPath row]; -
if (editingStyle == UITableViewCellEditingSt yleDelete) { -
[self.list removeObjectAtIndex:row]; -
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] -
withRowAnimation:UITableViewRowAnimationA utomatic]; -
}else if(editingStyle == UITableViewCellEditingSt yleInsert ){ -
NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil]; -
[self.list insertObject:@"inset new Cell" atIndex:row]; -
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationM iddle]; -
} - }
4.2 修改图标为插入样式。
- -
(UITableViewCellEditingSt yle)tableView:(UITableView *)tableView -
editingStyleForRowAtInde xPath:(NSIndexPath *)indexPath { -
return UITableViewCellEditingSt yleInsert; - }
- iOS学习之UITableView中Cell…
- iOS学习之UITableView中Cell的操作
- IOS学习之UITableView中Cell的操作
- iOS学习之UITableView中Cell的操作
- iOS中UITableView学习笔记(二)之cell重用
- IOS学习之UITableView中Cell的操作
- iOS学习之UITableView中Cell的操作
- iOS学习之UITableView中Cell的操作
- iOS学习之UITableView中Cell的操作
- iOS学习之UITableView…
- UITableView cell自定义视图中插入…
- iOS开发 UITableView之cell
- IOS之UITableView cell自定义视图中插入Tableview
- iOS开发之UITableView中Cell左滑自定义事件
- iPhone UItableView cell 自适应高…
- iOS之UITableView Cell重用机制
- iOS开发:XIB之UITableView自定义cell
- ios UITableView中Cell的重用机制
- 图文解释XCode常用快捷键的使用
- iOS学习之分段Table View的使…
- struts的OGNL(1)
- 如何上传图片到iOS模拟器
- 怎样为你电脑的xcode装上coc…
- iOS学习之UITableView中Cell…
- iOS学习之iOS5.0以上 使用新…
- iOS学习之iOS5.0以上使用新浪微博…
- iOS学习之UINavigationController…
- iOS学习之UINavigationController…
- ffmpeg convert audio
- iOS学习之UINavigationController…
- jQuery学习之:jqGrid表格插件——从servlet获得数据
- 配置Linux 开机启动服务