检查UIButton是否已经存在于UITableViewCell上

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

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

我认为你应该使用cellForRowAtIndexPath方法将任何子视图添加到单元格,以便在来回滚动时重用该单元格,并且在将子视图添加到单元格之前不需要任何检查。

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

为了避免button在离开屏幕时丢失状态,可以使用多个重用标识符,并将button的状态存储在数组中。 我们将调用这个属性buttonStates (它应该是一个可变数组)。

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

当状态改变时(这可能发生在copyPressedFromCell:方法中,用代表新状态的string更新button标记索引处的对象(已经设置为单元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]; } 

为什么你运行,如果陈述? 如果您刚刚创build了一个新的图像视图,那么每次都会返回YES (假设子视图数组默认不nil )。 您宁愿检查单元格子视图中是否已经存在底部单元格图像。 但我们甚至不需要这样做。 只要在!cell中的cellForRowAtIndexPath:方法中添加图像即可。 这样,如果单元格尚未创build,我们只添加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; } 

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

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

试试以下方法

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

希望它可以帮助你。