UITableViewCell

UITableView的每一行都是一個UITableViewCell,經過dataSource的tableView:cellForRowAtIndexPath:方法來初始化每一行
UITableViewCell是UIView的子類,內部有個默認的子視圖:contentView。contentView是UITableViewCell所顯示內容的父視圖,並負責顯示一些輔助指示視圖。輔助指示視圖的做用是顯示一個表示動做的圖標,能夠經過設置UITableViewCell的accessoryType來顯示,默認是UITableViewCellAccessoryNone(不顯示輔助指示視圖),其餘值以下:數組

  • UITableViewCellAccessoryDisclosureIndicator
  • UITableViewCellAccessoryDetailDisclosureButton
  • UITableViewCellAccessoryCheckmark

UITableViewCell的contentViewide

  • contentView下默認有3個子視圖,其中的2個是UILabel(經過UITableViewCell的textLabel和detailTextLabel屬性訪問),第3個是UIImageView(經過UITableViewCell的imageView屬性訪問)
  • UITableViewCell還有一個UITableViewCellStyle屬性,用於決定使用contentView的哪些子視圖,以及這些子視圖在contentView中的位置:
  1. UITableViewCellStyleDefault
  2. UITableViewCellStyleSubtitle
  3. UITableViewCellStyleValue1
  4. UITableViewCellStyleValue2

UITableViewCell結構佈局

UITableViewCell對象的重用原理動畫

  • iOS設備的內存有限,若是用UITableView顯示成千上萬條數據,就須要成千上萬個UITableViewCell對象的話,那將會耗盡iOS設備的內存。要解決該問題,須要重用UITableViewCell對象
  • 重用原理:當滾動列表時,部分UITableViewCell會移出窗口,UITableView會將窗口外的UITableViewCell放入一個對象池中,等待重用。當UITableView要求dataSource返回UITableViewCell時,dataSource會先查看這個對象池,若是池中有未使用的UITableViewCell,dataSource會用新的數據配置這個UITableViewCell,而後返回給UITableView,從新顯示到窗口中,從而避免建立新對象

 

  • 還有一個很是重要的問題:有時候須要自定義UITableViewCell(用一個子類繼承UITableViewCell),並且每一行用的不必定是同一種UITableViewCell(如短信聊天佈局),因此一個UITableView可能擁有不一樣類型的UITableViewCell,對象池中也會有不少不一樣類型的UITableViewCell,那麼UITableView在重用UITableViewCell時可能會獲得錯誤類型的UITableViewCell
  • 解決方案:UITableViewCell有個NSString *reuseIdentifier屬性,能夠在初始化UITableViewCell的時候傳入一個特定的字符串標識來設置reuseIdentifier(通常用UITableViewCell的類名)。當UITableView要求dataSource返回UITableViewCell時,先經過一個字符串標識到對象池中查找對應類型的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被選中時的背景顏色:對象

  • UITableViewCellSelectionStyleNone 沒有顏色
  • UITableViewCellSelectionStyleBlue 藍色(默認)
  • UITableViewCellSelectionStyleGray 灰色

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 }
相關文章
相關標籤/搜索