我有ReusableCellWithIdentifier
的tableview我正在寻找最佳解决方案。
我不想使用 removedubview 方法,如下所示
if ([cell.contentView subviews]){
for (UIBUtton *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
这是我的cellForRowAtIndexPath方法。 我习惯使用tag属性获取Button。但是在滚动的同时,在diff部分的diff行中添加了此按钮。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdetifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdetifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetifier];
}
if ((indexPath.section==1 && indexPath.row==0) || (indexPath.section==3 && indexPath.row==1)) {
cell.accessoryView = [self buttonWithMap:cell];
}
return cell;
}
创建Buttonn
- (UIButton *)buttonWithMap:(UITableViewCell *)cell
{
UIButton *btn=(UIButton *)[cell.contentView viewWithTag:101];
if (btn) {
return btn;
}
else{
UIButton *btnLocation = [UIButton buttonWithType:UIButtonTypeCustom];
btnLocation.backgroundColor = [UIColor clearColor];
btnLocation.frame = CGRectMake(0, 0, 20, 20);
[btnLocation setImage:[UIImage imageNamed:@"map_location.png"] forState:UIControlStateNormal];
[btnLocation addTarget:self action:@selector(setAddressBySelectLocation:) forControlEvents:UIControlEventTouchUpInside];
[btnLocation setTag:101];
return btnLocation;
}
}
答案 0 :(得分:1)
我相信你需要在条件不满足时将其设置为nil,否则tableView
会混淆:
if ((indexPath.section==1 && indexPath.row==0) || (indexPath.section==3 && indexPath.row==1)) {
cell.accessoryView = [self buttonWithMap:cell];
}else{
cell.accessoryView = nil;
}