填充 TableView Cell 的正确方法
Correct way to populate TableView Cell
自 2009 年以来,这是我填充 tableView 单元格(自定义单元格)的方式:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountCellID"];
if (cell==nil) {
cell = [[AccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AccountCellID"];
}
// here update content
cell.customImg = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png",indexPath.row]]
return cell;
}
这样好还是有更好的方法?有人说我应该更新 TableViewcell 自定义 class 本身中的所有内容。有什么不同吗?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return Array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FilterTableViewCell *cell = (FilterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"filter cell"];
BrandObjectClass *brandObject = [brandsArray objectAtIndex:indexPath.row];
cell.lblFilterName.text = brandObject.brandName;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.lblFilterName.font = [UIFont fontWithName:@"Roboto-Medium" size:cell.lblFilterName.font.pointSize];
cell.imgTick.hidden = NO;
return cell;
}
当您使用自定义 table 视图单元格时,转换也很重要,因为 dequeueReusableCellWithIdentifier
returns UITableViewCell
的对象。
希望这会有所帮助。
自 2009 年以来,这是我填充 tableView 单元格(自定义单元格)的方式:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountCellID"];
if (cell==nil) {
cell = [[AccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AccountCellID"];
}
// here update content
cell.customImg = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png",indexPath.row]]
return cell;
}
这样好还是有更好的方法?有人说我应该更新 TableViewcell 自定义 class 本身中的所有内容。有什么不同吗?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return Array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FilterTableViewCell *cell = (FilterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"filter cell"];
BrandObjectClass *brandObject = [brandsArray objectAtIndex:indexPath.row];
cell.lblFilterName.text = brandObject.brandName;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.lblFilterName.font = [UIFont fontWithName:@"Roboto-Medium" size:cell.lblFilterName.font.pointSize];
cell.imgTick.hidden = NO;
return cell;
}
当您使用自定义 table 视图单元格时,转换也很重要,因为 dequeueReusableCellWithIdentifier
returns UITableViewCell
的对象。
希望这会有所帮助。