iOS学习之UITableView…
2013-10-17 13:26
411 查看
原文地址:iOS学习之UITableView中Cell的操作"> iOS学习之UITableView中Cell的操作作者:龍之殇
接着iOS学习之Table
View的简单使用 这篇,这里主要讲UITableView
中的Cell的操作,包括标记、移动、删除、插入。
为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http://download.csdn.net/detail/totogo2010/4361870
要进行数据的操作了,把代码里的不可变数组改成可变的:
NSArray *list
-》NSMutableArray *list
效果如下:
iOS学习之UITableView中Cell的操作" />
打开项目,
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath。
添加代码
[cpp] view
plaincopy
-(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 == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
标记分别有四种效果:
UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone
可以自己试试。
想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。
打开xib,生成Table的IBoutlet映射 tableView;
在viewDidload里添加
[self.tableViewsetEditing:YES];
这是启动运行程序,
iOS学习之UITableView中Cell的操作" />
打开可编辑模式,默认情况显示删除的图标的。
实现删除的代码:
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete
在这删除行的方法又出现了一个常量:UITableViewRowAnimationAutomatic,它表示删除时的效果,类似的常量还有:
UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone
从常量名称打开可以看出效果来。
这是运行,就可以删除其中的一个Cell行了。
添加代码如下:
3.1先把默认的删除的图标去掉
[cpp] view
plaincopy
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}
3.2返回当前Cell是否可以移动
[cpp] view
plaincopy
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
3.3执行移动操作
[cpp] view
plaincopy
- (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];
}
运行程序:
iOS学习之UITableView中Cell的操作" />
怎么移动呢?不要以为按住行的任何地方都能移动,要按住最左边的三道杠的图标才能拖动移动
4.1插入和删除差不多,在
- (void)tableView:(UITableView *)tableView
commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
添加UITableViewCellEditingStyleInsert判断
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}else if(editingStyle == UITableViewCellEditingStyleInsert ){
NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
[self.list insertObject:@"inset new Cell" atIndex:row];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
}
}
4.2 修改图标为插入样式。
[cpp] view
plaincopy
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}
运行,点加号图标,
iOS学习之UITableView中Cell的操作" />
完成!
接着iOS学习之Table
View的简单使用 这篇,这里主要讲UITableView
中的Cell的操作,包括标记、移动、删除、插入。
为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http://download.csdn.net/detail/totogo2010/4361870
要进行数据的操作了,把代码里的不可变数组改成可变的:
NSArray *list
-》NSMutableArray *list
1、标记Cell。
效果如下:iOS学习之UITableView中Cell的操作" />
打开项目,
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath。
添加代码
[cpp] view
plaincopy
-(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 == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
标记分别有四种效果:
UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone
可以自己试试。
2、删除Cell
想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。打开xib,生成Table的IBoutlet映射 tableView;
在viewDidload里添加
[self.tableViewsetEditing:YES];
这是启动运行程序,
iOS学习之UITableView中Cell的操作" />
打开可编辑模式,默认情况显示删除的图标的。
实现删除的代码:
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
这个方法根据参数editingStyle是UITableViewCellEditingStyleDelete
在这删除行的方法又出现了一个常量:UITableViewRowAnimationAutomatic,它表示删除时的效果,类似的常量还有:
UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone
从常量名称打开可以看出效果来。
这是运行,就可以删除其中的一个Cell行了。
3、移动Cell
添加代码如下:3.1先把默认的删除的图标去掉
[cpp] view
plaincopy
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}
3.2返回当前Cell是否可以移动
[cpp] view
plaincopy
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
3.3执行移动操作
[cpp] view
plaincopy
- (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];
}
运行程序:
iOS学习之UITableView中Cell的操作" />
怎么移动呢?不要以为按住行的任何地方都能移动,要按住最左边的三道杠的图标才能拖动移动
4、插入cell:
4.1插入和删除差不多,在- (void)tableView:(UITableView *)tableView
commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
添加UITableViewCellEditingStyleInsert判断
[cpp] view
plaincopy
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.list removeObjectAtIndex:row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}else if(editingStyle == UITableViewCellEditingStyleInsert ){
NSArray *insertIndexPaths = [NSArray arrayWithObjects:indexPath,nil];
[self.list insertObject:@"inset new Cell" atIndex:row];
[tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];
}
}
4.2 修改图标为插入样式。
[cpp] view
plaincopy
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleInsert;
}
运行,点加号图标,
iOS学习之UITableView中Cell的操作" />
完成!
相关文章推荐
- iOS学习之UITableView中Cell…
- iOS学习之Table View的简单使用
- iOS学习之分段Table View的使…
- IOS UITableView NSInde…
- IOS 如何让UITableVIew的行纪录 不…
- iOS开发学习笔记——表格4(UITableView)->自定义单元格(UITableViewCll)
- iOS学习之Table View的…
- IOS UITableView 上滑 下拉刷新 可…
- IOS UITableView 学习记录
- IOS UITableView学习Demo
- android 动画学习之ViewFlipper
- IOS学习之路十六(UItableView 通过Prepare for segue 页面传值)
- 我的iOS学习历程 - UITableView(数据model的使用)
- iOS——button&&imageview的学习笔记
- IOS UI view button 自定义颜色………
- iOS UIscrollView的设计模式
- (素材源码)猫猫学IOS(十三)UI之UITableView学习(下)汽车名牌带右侧索引
- IOS开发学习笔记028-UITableView单组数据显示代码优化
- iOS学习笔记之UITableView(1)
- iOS菜鸟学习——去除UITableView中多余的separator