【发布时间】:2013-02-07 13:11:45
【问题描述】:
对于每个UITableViewCell,我在tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath 方法中创建一个UIButton。我想在创建它时检查UIButton是否已经存在,所以它没有被多次添加,但我不能。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
UIImageView *cellBottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, CELL_HEIGHT, 320.0, 1)];
cellBottomLine.image = [UIImage imageNamed:@"bottomCellImage.png"];
if (cellBottomLine.subviews) {
[cell addSubview:cellBottomLine];
}
copyButton = [BSFunctions createButtonWithNormalImageNamed:@"copyCellButton.png" highlightedImageName:@"copyCellButtonPressed.png" target:self selector:@selector(copyPressedFromCell:)];
[copyButton setFrame:CGRectMake(250, 10, 62, 32.5)];
copyButton.tag = indexPath.row;
if (!copyButton.superview) {
[cell addSubview:copyButton];
}
return cell;
}
【问题讨论】:
-
为什么不在
cellForRowAtIndexPath:方法中添加按钮? -
我会接受 Kuba 的建议,您可以在检查单元格是否不为零的同时添加按钮,这样也不会产生冗余。
标签: iphone ios objective-c cocoa-touch uitableview