標籤:
1、效能最佳化(添加幾百個cell到view) 2、如何?選中某行,改變這個cell最右側顯示的對號按鈕
1、如果表格中又幾百條資料的話,系統會自動載入顯示在介面上得資料,逐一載入
添加100個資料到UITableView中
1 for (int i = 0 ; i < 100 ; i ++)2 {3 NSString *icon = [NSString stringWithFormat:@"00%d.png",arc4random_uniform(8) + 1];4 NSString *name = [NSString stringWithFormat:@"第%d",i];5 NSString *desc = [NSString stringWithFormat:@"第%d行的描述",i];6 Shop *tmp = [Shop shopWithIcon:icon andName:name andDesc:desc];7 [_shops addObject:tmp];8 9 }
在滑動螢幕進行顯示的時候,只會載入當前螢幕中顯示的資料。
1 // 設定行內容 2 // 每當有一個cell進入視野範圍內就會調用 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 4 { 5 UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"]; 6 cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row]; 7 NSLog(@"%p,第%ld行資料",cell,indexPath.row); 8 9 return cell;10 }
介面中只顯示了三個cell,如,向下滑動,每次超過三個時就載入新的cell,向上滑動會重新載入cell,而且每次都會重新申請記憶體.
如果想避免這種情況可以使用緩衝池,這是UITableViewCell 內建的方法 dequeueReusableCellWithIdentifier
1 // 設定行內容 2 // 每當有一個cell進入視野範圍內就會調用 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 4 { 5 // 從緩衝池中選擇可迴圈利用的cell,指定標識c1,這樣就會找到結構一樣的cell 6 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c1"]; 7 // 如果緩衝池中沒有 8 if (cell == nil) 9 {10 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"]; // 設定標識C111 }12 // UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"];13 cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];14 NSLog(@"%p,第%ld行資料",cell,indexPath.row);15 16 return cell;17 }
看運行結果
介面開始顯示三個cell,向下滑動時會有一個過渡這回建立一個cell,但是接著往下就會使用已經存在的cell,從第四行開始使用第0行建立的cell
原始碼:http://pan.baidu.com/s/1i3qyAjj
2、如何?選中某行,改變這個cell最右側顯示的對號按鈕
選中某行和取消選中某行
1、選中某行執行方法
1 // 選中某行執行 2 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 NSLog(@"selected"); 5 //選中後顏色變深 6 // 在最右側顯示一個對號表徵圖 7 // 1、獲得選中行 8 Shop *s = _shops[indexPath.row]; 9 // 2、修改選中行的資料,將選中的cell添加到待刪除數組中10 if ([_deleteShops containsObject:s]) // 如果已經存在,再次點擊就取消選中按鈕11 {12 [_deleteShops removeObject:s];13 }14 else // 否則就添加待刪除數組15 {16 [_deleteShops addObject:s];17 }18 // 3、更新資料,更新資料也就是重新設定某一行的內容19 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];20 21 }
2、取消選中某行
1 // 取消選中某行執行2 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath3 {4 NSLog(@"Deselected");5 }
3、重新設定選中行的內容
1 // 設定行內容 2 // 每當有一個cell進入視野範圍內就會調用 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 4 { 5 static NSString *ID = @"C1"; 6 // 從緩衝池中選擇可迴圈利用的cell,指定標識c1,這樣就會找到結構一樣的cell 7 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; 8 // 如果緩衝池中沒有 9 if (cell == nil)10 {11 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; // 設定標識C112 }13 // UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"];14 // 更新資料到介面15 Shop *s = _shops[indexPath.row];16 cell.textLabel.text = s.name;17 cell.imageView.image = [UIImage imageNamed:s.icon];;18 cell.detailTextLabel.text = s.desc;19 // 顯示最右側的按鈕20 if ([_deleteShops containsObject:s]) // 判斷是否已經選中的cell,是得話設定表徵圖21 {22 cell.accessoryType = UITableViewCellAccessoryCheckmark;23 }24 else // 否則就什麼都不顯示25 {26 cell.accessoryType = UITableViewCellAccessoryNone;27 }28 29 // NSLog(@"%p,第%ld行資料",cell,indexPath.row);30 31 return cell;32 }
代碼中使用一個新的數組來儲存選中的行_deleteShops,並在更新資料事進行判斷。
4、載入圖片和文字使用一個plist檔案
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view, typically from a nib. 5 6 // 讀取*.plist檔案 7 // 1.擷取全路徑 8 NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"]; 9 // 2.讀取資料到數組10 NSArray *array = [NSArray arrayWithContentsOfFile:path];11 // 初始化數組12 _shops = [NSMutableArray array];13 _deleteShops = [NSMutableArray array];14 //NSLog(@"%d",array.count);15 // 添加資料到介面16 for (NSDictionary *arr in array)17 {18 // 1.建立shop19 Shop *s = [Shop shopWithDict:arr];20 // 2.添加到數組21 [_shops addObject:s];22 }23 24 }
5、shop模型進行了其他一些修改,增減一個類方法和一個對象方法用於返回Shop對象
1 - (id)initWithDict:(NSDictionary *)dict 2 { 3 Shop *shop = [[Shop alloc] init]; 4 shop.icon = dict[@"icon"]; 5 shop.name = dict[@"name"]; 6 shop.desc = dict[@"desc"]; 7 return shop; 8 } 9 + (id)shopWithDict:(NSDictionary *)dict10 {11 return [[self alloc] initWithDict:dict];12 }
效果
原始碼: http://pan.baidu.com/s/1mgxKgMO
IOS開發學習筆記028-UITableView單組資料顯示代碼最佳化