从UITableViewCell中删除UIButton

时间:2012-03-13 22:37:57

标签: objective-c cocoa-touch uitableview

我从UITableViewCell的子类中删除UIButton时遇到了问题。仅当单元格是表格视图中的第一个单元格时,才会添加此按钮。所以碰巧这个按钮可以是nil,或者它可以是UIButton类的一个实例。此外,由于所有这些单元格具有相同的标识符,因此可能存在第一个单元格(带按钮)向下移动的情况。然后,我需要删除此按钮。

我是这样做的:

if(callBtn != nil) {
    [callBtn removeFromSuperview];
}

但是,它会导致应用程序崩溃。

我想这个问题可以通过为第一个单元格和其他单元格使用不同的标识符来克服,可能它是一个更好的解决方案。但是,我想知道这个代码有什么问题,或者从UITableViewCell的子类中删除子视图时我应该注意什么。

@EDIT: 以下是创建单元格的代码:

NSString *ident = @"HistoryCell";
HistoryItemCell *cell = (HistoryItemCell *)[tableView dequeueReusableCellWithIdentifier:ident];
// If there is no reusable cell of this type, create a new one
if (!cell) {
    if(indexPath.row == 0) {
        cell = [[[HistoryItemCell alloc] initWithStyle:UITableViewCellStyleDefault withCallBtn:YES reuseIdentifier:ident] autorelease];
    } else {
        cell = [[[HistoryItemCell alloc] initWithStyle:UITableViewCellStyleDefault withCallBtn:NO reuseIdentifier:ident] autorelease];
    }
} else {
    if(indexPath.row != 0) {
        [cell removeCallBtn];
    }
}

History *history = [[[Store defaultStore] allHistories] objectAtIndex:indexPath.row];
[cell setDataFromModel:history];
return cell;

添加按钮代码:

if(withCallBtn == YES) {
        callBtn = [[UIButton alloc] initWithFrame:CGRectZero];
        callBtn.tag = CALL_BUTTON_TAG;
        [callBtn addTarget:self action:@selector(callBtnAction:) forControlEvents:UIControlEventTouchUpInside];

        // setting background, title, etc

        [self.contentView addSubview:callBtn];
        [callBtn release];
    }

问候, 亚当

0 个答案:

没有答案
相关问题