TableView cell的循環(huán)利用的三種方式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//設置重用標識符
static NSString *ID = @"cell";
//根據cell的標識符去緩存池中查找對應的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//如果cell為nil(緩存池中不存在對應的cell)衡奥,則創(chuàng)建cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
//覆蓋原來cell的數據
cell.textLabel.text = @"new data";
return cell;
}
//設置一個全局的重用標識符
NSString *ID = @"cell";
//注冊標識符對應的cell爹袁,cell類型是默認類型,且不能修改矮固,并且只能注冊一次。建議在-(void)viewDidLoad中注冊
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//在緩存池中查找標識符對應的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//覆蓋原來cell的數據
cell.textLabel.text = @"new data";
return cell;
}
3.在TableViewController中使用
修改cell對應的identifier
修改之后
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//設置重用標識符
static NSString *ID = @"cell";
//根據cell的標識符去緩存池中查找對應的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//覆蓋原來cell的數據
cell.textLabel.text = @"new data";
return cell;
}