检查的UIButton上的UITableViewCell已经存在(Check if UIButton

2019-11-01 12:51发布

对于每一个UITableViewCell我创建一个UIButtontableView:(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;
}

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


Answer 2:

我认为你应该使用cellForRowAtIndexPath来回滚动时方法添加任何子视图细胞,使细胞将被重用,你不要需要增加子视图电池之前的任何检查。



Answer 3:

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


Answer 4:

为了完全避免这个,为什么不继承UITableViewCell ,并在情节提要/ XIB创建该类的一个指定的代表性? 这样,当细胞被重复使用,操作系统也不会一次又一次地创建UI元素,而是重新使用已经从IBOutlets分配的。



Answer 5:

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


Answer 6:

尝试下面的方法

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

希望它可以帮助你。



文章来源: Check if UIButton already exists on UITableViewCell