iOS学习之UITableView中Cell…

来源:互联网 发布:阿里云地域可以更换吗 编辑:程序博客网 时间:2024/06/06 10:38

接着iOS学习之TableView的简单使用 这篇,这里主要讲UITableView中的Cell的操作,包括标记、移动、删除、插入。

 

为了简单快捷,直接从原来那篇的代码开始,代码下载地址:http://download.csdn.net/detail/totogo2010/4361870

要进行数据的操作了,把代码里的不可变数组改成可变的:

NSArray *list-》NSMutableArray *list 

1、标记Cell。

效果如下:


打开项目,

 

-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath。

添加代码

 

[cpp] viewplaincopy
  1. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{  
  2. //    NSString *rowString [self.list objectAtIndex:[indexPath row]];  
  3. //    UIAlertView alter [[UIAlertView alloc] initWithTitle:@"选中的行信息" message:rowString delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  4. //    [alter show];  
  5.     UITableViewCell *cell [tableView cellForRowAtIndexPath:indexPath];  
  6.     if (cell.accessoryType == UITableViewCellAccessoryNone)  
  7.         cell.accessoryType UITableViewCellAccessoryCheckmark;  
  8.     }else  
  9.         cell.accessoryType UITableViewCellAccessoryNone;  
  10.      
  11.     [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  12.  
标记分别有四种效果:

 

UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryNone

可以自己试试。

2、删除Cell

想要实现移动或者删除行这样的操作,需要启动表格的编辑模式。使用的是setEditing:animated:方法。

打开xib,生成Table的IBoutlet映射  tableView;

在viewDidload里添加

   [self.tableViewsetEditing:YES];

这是启动运行程序,

打开可编辑模式,默认情况显示删除的图标的。

实现删除的代码:

 

[cpp] viewplaincopy
  1. (void)tableView:(UITableView *)tableView commitEditingStyle:  
  2. (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
  3.     NSUInteger row [indexPath row];  
  4.     if (editingStyle == UITableViewCellEditingStyleDelete)  
  5.         [self.list removeObjectAtIndex:row];   
  6.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
  7.                          withRowAnimation:UITableViewRowAnimationAutomatic];   
  8.      
  9.  

 

这个方法根据参数editingStyleUITableViewCellEditingStyleDelete

在这删除行的方法又出现了一个常量:UITableViewRowAnimationAutomatic,它表示删除时的效果,类似的常量还有:
UITableViewRowAnimationAutomatic
UITableViewRowAnimationTop
UITableViewRowAnimationBottom
UITableViewRowAnimationLeft
UITableViewRowAnimationRight
UITableViewRowAnimationMiddle
UITableViewRowAnimationFade
UITableViewRowAnimationNone

从常量名称打开可以看出效果来。

这是运行,就可以删除其中的一个Cell行了。

3、移动Cell

添加代码如下:

3.1先把默认的删除的图标去掉

 

[cpp] viewplaincopy
  1. (UITableViewCellEditingStyle)tableView:(UITableView *)tableView  
  2.            editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath   
  3.     return UITableViewCellEditingStyleInsert;   
  4.   

 

3.2返回当前Cell是否可以移动

 

[cpp] viewplaincopy
  1. (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath   
  2.     return YES;   
  3.  
3.3执行移动操作

 

 

[cpp] viewplaincopy
  1. (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)  
  2. sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath  
  3.     NSUInteger fromRow [sourceIndexPath row];   
  4.     NSUInteger toRow [destinationIndexPath row];   
  5.       
  6.     id object [self.list objectAtIndex:fromRow];   
  7.     [self.list removeObjectAtIndex:fromRow];   
  8.     [self.list insertObject:object atIndex:toRow];   
  9.  
运行程序:

 


怎么移动呢?不要以为按住行的任何地方都能移动,要按住最左边的三道杠的图标才能拖动移动

4、插入cell:

4.1插入和删除差不多,在

- (void)tableView:(UITableView *)tableViewcommitEditingStyle:

(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath

添加UITableViewCellEditingStyleInsert判断

 

[cpp] viewplaincopy
  1. (void)tableView:(UITableView *)tableView commitEditingStyle:  
  2. (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath  
  3.     NSUInteger row [indexPath row];  
  4.     if (editingStyle == UITableViewCellEditingStyleDelete)  
  5.         [self.list removeObjectAtIndex:row];   
  6.         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  
  7.                          withRowAnimation:UITableViewRowAnimationAutomatic];   
  8.     }else if(editingStyle == UITableViewCellEditingStyleInsert ){  
  9.         NSArray *insertIndexPaths [NSArray arrayWithObjects:indexPath,nil];  
  10.         [self.list insertObject:@"inset new Cell" atIndex:row];  
  11.         [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationMiddle];  
  12.      
  13.  

4.2 修改图标为插入样式。

 

 

[cpp] viewplaincopy
  1. (UITableViewCellEditingStyle)tableView:(UITableView *)tableView  
  2.            editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath   
  3.     return UITableViewCellEditingStyleInsert;   
  4.   
运行,点加号图标,

 



完成!