添加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,並且每次都會從新申請內存.dom
若是想避免這種狀況可使用緩存池,這是UITableViewCell 自帶的方法 dequeueReusableCellWithIdentifierspa
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"]; // 設定標識C1 11 } 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 }
看運行結果code
界面開始顯示三個cell,向下滑動時會有一個過渡這回新建一個cell,可是接着往下就會使用已經存在的cell,從第四行開始使用第0行建立的cellorm
源代碼:http://pan.baidu.com/s/1i3qyAjj 對象
選中某行和取消選中某行blog
1 // 選中某行執行 2 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 NSLog(@"selected"); 5 //選中後顏色變深 6 // 在最右側顯示一個對號圖標 7 // 一、得到選中行 8 Shop *s = _shops[indexPath.row]; 9 // 二、修改選中行的數據,將選中的cell添加到待刪除數組中 10 if ([_deleteShops containsObject:s]) // 若是已經存在,再次點擊就取消選中按鈕 11 { 12 [_deleteShops removeObject:s]; 13 } 14 else // 不然就添加待刪除數組 15 { 16 [_deleteShops addObject:s]; 17 } 18 // 三、更新數據,更新數據也就是從新設置某一行的內容 19 [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 20 21 }
1 // 取消選中某行執行 2 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 3 { 4 NSLog(@"Deselected"); 5 }
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]; // 設定標識C1 12 } 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,並在更新數據事進行判斷。圖片
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.建立shop 19 Shop *s = [Shop shopWithDict:arr]; 20 // 2.添加到數組 21 [_shops addObject:s]; 22 } 23 24 }
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 *)dict 10 { 11 return [[self alloc] initWithDict:dict]; 12 }
效果如圖: 內存