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 }