检查UITutViewCell上是否已存在UIButton

时间:2013-02-07 13:11:45

标签: iphone ios objective-c cocoa-touch uitableview

对于每个UITableViewCell我在UIButton方法中创建tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath。我想在创建时检查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;
}

6 个答案:

答案 0 :(得分:1)

我认为您应该使用cellForRowAtIndexPath方法将任何子视图添加到单元格中,以便在向子网单元添加子视图之前来回滚动时不会需要进行任何检查。

答案 1 :(得分:1)

- (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];
    }

    for (UIView *view in [cell subviews]) 
    {
        if ([view isKindOfClass:[UIButton class]]) 
        {
             return cell;
        }
    }

    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;
}

答案 2 :(得分:1)

Stavash的答案是最好的,但如果您不想这样做,或者您可以使用tableView:cellForRowAtIndexPath:。我看到你试过了,滚动时按钮的状态丢失了。我认为这是由于细胞重用。使用单元格重用时,一旦表格视图单元格离开屏幕,它就会被标识符缓存,然后屏幕上的下一个单元格将从缓存中检索具有给定标识符的单元格。

为避免按钮在离开屏幕时失去其状态,您可以使用多个重用标识符并将按钮的状态存储在数组中。我们将此属性称为buttonStates(它应该是一个可变数组)。

初始化您的buttonStates变量并添加字符串以表示“基本状态”。添加尽可能多的这些字符串作为表格中的单元格数量(我假设只有一个部分基于您的代码)。

当状态发生变化时(这可能发生在你的copyPressedFromCell:方法中,更新按钮标记索引处的对象(已经设置为单元格的索引),并使用表示新的字符串的字符串状态。

完成所有操作后,请将cellForRowAtIndexPath:方法设为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = self.buttonStates[indexPath.row];
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

在我们继续之前,让我们看一下您的willDisplayCell:代码(应该移到这里)。

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];
}

为什么要运行if语句?如果您刚刚创建了一个新的图像视图,那么每次都会返回YES(假设默认情况下子视图数组不是nil)。您更愿意检查单元格子视图中是否已存在底部单元格图像。但我们甚至不需要这样做。只需在cellForRowAtIndexPath:方法内的!cell方法中添加图片即可。这样,如果尚未创建单元格,我们只会添加bottomCellImage

同样适用于if (!copyButton.superview)电话。继续我们离开的cellForRowAtIndexPath:方法:

        UIImageView *cellBottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, CELL_HEIGHT, 320.0, 1)];
        cellBottomLine.image = [UIImage imageNamed:@"bottomCellImage.png"];
        [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;
        [cell addSubview:copyButton];
    }

    UIButton *button = [cell viewWithTag:indexPath.row]; // Get the copy button
    // Update the state of the button here based on CellIdentifier, outside the if statement so that it gets updated no matter what
    // Also configure your cell with any non-button related stuffs here

    return cell;
}

答案 3 :(得分:0)

为了完全避免这种情况,为什么不将UITableViewCell子类化并在storyboard / XIB中创建该类的指定表示?这样,当重用单元格时,操作系统不会一次又一次地创建UI元素,而是重用已经从IBOutlets分配的元素。

答案 4 :(得分:0)

you can check it in this way:
1) put somewhere `copyButton = nil;` (in viewDidLoad, or somewhere else;
2) then:
- (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];
    }
    if (!copyButton) {
        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;
}

答案 5 :(得分:0)

尝试以下方式

if ([cell viewWithTag:LBL_TAG])
{
    UIView *vi = (UIView*)[cell viewWithTag:LBL_TAG];
    if ([vi isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel*)vi;
        NSLog(@"test  %@..",lbl.text);
    }
}

希望它可以帮到你。

相关问题