UITableView的每一行都是一个UITableViewCell,经过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行
UITableViewCell是UIView的子类,内部有个默认的子视图:contentView。contentView是UITableViewCell所显示内容的父视图,并负责显示一些辅助指示视图。辅助指示视图的做用是显示一个表示动做的图标,能够经过设置UITableViewCell的accessoryType来显示,默认是UITableViewCellAccessoryNone(不显示辅助指示视图),其余值以下:数组
UITableViewCell的contentViewide
UITableViewCell结构布局
UITableViewCell对象的重用原理动画
重用UITableViewCell对象spa
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 2 static NSString *identifier = @"UITableViewCell"; 3 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 4 if (cell == nil) { 5 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 6 } 7 cell.textLabel.text = [NSString stringWithFormat:@"Text %i", indexPath.row]; 8 return cell; 9 }
UITableViewCell的经常使用属性code
//设置背景
backgroundView
//设置被选中时的背景视图
selectedBackgroundVieworm
selectionStyle属性可设置UITableViewCell被选中时的背景颜色:对象
UITableView的编辑模式blog
1 #pragma mark 编辑提交方法 2 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 //NSLog(@"commitingStyle"); 5 if(editingStyle == UITableViewCellEditingStyleDelete){ 6 //删除操做 7 //1.从数组中删除数据 8 [self.myData removeObjectAtIndex:indexPath.row]; 9 //2.刷新表格数据,以动画方式 10 //[self.tableView reloadData]; 11 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft]; 12 }else{ 13 //1.把数据添加到数组中 14 NSString *name = [NSString stringWithFormat:@"添加新数据"]; 15 Project *p = [Project projectWithName:name]; 16 [self.myData insertObject:p atIndex:indexPath.row +1]; 17 //2.刷新表格数据,以动画方式 18 //[self.tableView reloadData]; 19 NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row +1 inSection:0]; 20 [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]; 21 } 22 23 } 24 #pragma mark - 拖动排序 25 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 26 { 27 NSLog(@"from %d to %d",sourceIndexPath.row,destinationIndexPath.row); 28 //1.取出要移动的数据 29 Project *p = self.myData[sourceIndexPath.row]; 30 //2.删除sourceIndexPath数据 31 [self.myData removeObjectAtIndex:sourceIndexPath.row]; 32 //3.添加到destinationIndexPath数据 33 [self.myData insertObject:p atIndex:destinationIndexPath.row]; 34 35 } 36 37 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 38 { 39 //NSLog(@"editingStyle...."); 40 return tableView.tag; 41 } 42 43 #pragma mark - 删除和添加方法 44 #pragma mark 删除方法 45 -(IBAction)removeRow 46 { 47 //删除时,设置tableView.tag = UITableViewCellEditingStyleDelete 48 self.tableView.tag = UITableViewCellEditingStyleDelete; 49 50 BOOL edit = self.tableView.editing; 51 [self.tableView setEditing:!edit]; 52 //NSLog(@"yyh123..."); 53 } 54 55 56 #pragma mark 添加方法 57 - (void)addRow 58 { 59 //添加时,self.tableView.tag = UITableViewCellEditingStyleInsert 60 self.tableView.tag = UITableViewCellEditingStyleInsert; 61 BOOL edit = self.tableView.editing; 62 [self.tableView setEditing:!edit]; 63 }