我接手了一个 iOS 项目,必须将 View 列表重构为 UITableView。我正在使用 Storyboards 并将 UITableViewCell 子类化。一个子类称为 MenuItemCell,它有一个 headerLabel、detailLabel 和 priceLabel,它们是在 Storyboard 中设置并在 MenuItemCell 中配置的属性。我可以像这样通过 cellForAtIndexPath 操作它们:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MenuItemCellIdentifier=@"MenuItemCell";
id dic=self.tmpMenu.listItems[indexPath.row];
if([dic isKindOfClass:[MenuItem class]]){
MenuItemCell *cell = [self.menuTV dequeueReusableCellWithIdentifier:MenuItemCellIdentifier];
MenuItem *menuItem=(MenuItem *)dic;
cell.menuItem=menuItem;
cell.headerLabel.text=menuItem.header;
cell.headerLabel.numberOfLines=0;
cell.priceLabel.text=menuItem.price;
// how to handle this custom spotView
if([menuItem hasInstoreImage]){
UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
[cell.spotView addSubview:instoreImageDot]; // ON SCROLLING, this populates to all the different table cells
}
return cell;
}
return nil;
}
最后一 block 是有一个自定义的UIView叫spotView。目前,我正在通过 circleWithColor 在我的 Controller 中的代码中创建这个圆圈并尝试添加到 [cell.spotView] 但滚动导致它填充在不同的表格单元格上。我应该如何设置?我已经向我的自定义 View 添加了一个方法,但这也遇到了同样的问题。
最佳答案
单元格被重用,您需要告诉 tableView 删除自定义 View
if([menuItem hasInstoreImage]){
UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
[cell.spotView addSubview:instoreImageDot];
}else{
//remove it if condition is not met
//or You can add a place holder view instead
}
关于ios - 如何设置这个自定义 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410711/