![](/img/trans.png)
[英]TextField first cell result shows in other cells in Tableview cell
[英]Button added to tableview cell appears on other random cells as well
我在将按钮添加到表格单元格时遇到问题。 基本上,我将按钮添加到特定的单元格,但是该按钮还会出现在随机的其他单元格上(当前不在视图中)。
当我点击一个单元格时,我设置了一个变量( tappedCell=indexPath
),然后重新加载该单元格。 在我的cellForRowAtIndexPath
我检查indexPath
是否等于tappedCell
,如果是,则向其中添加一个按钮。 可以,但是此按钮也会出现在tableView
更下方的其他单元格上。 当我点击这些单元格时,它们不在视图中,而是在更下方,因此我必须滚动查看它们。 如果我不断(迅速)上下滚动,则会出现越来越多带有按钮的单元格。 这似乎是完全随机的。
我试过在if-statement
中添加按钮的地方添加NSLog()
,但这不会被调用一次以上(当我点击原始单元格时)。 因此,它实际上并没有添加更多按钮,它必须是出现在多个单元格中的相同按钮。
我不知道为什么会这样。 我尝试过向每个单元格添加一个按钮,然后将[self.tappedCell isEqual:indexPath]
设置为标准的hidden = YES
,并设置hidden = NO
,但这并不会改变任何内容。
在这里,您可以看到用于将按钮添加到单元格中的代码:
UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[newBtn setImage:[UIImage imageNamed:@"open.png"] forState:UIControlStateNormal];
[newBtn setFrame:CGRectMake(220, 0, 100, 86)];
[newBtn addTarget:self action:@selector(openImage:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:newBtn];
我不知道自己在做什么错,或者根本不做错什么。 值得一提的是,我使用的是标准的Apple / iOS style=subtitle
单元,而不是自定义单元。 这是为了在我的整个应用程序中保持一致(这些单元是在情节提要中制作的,而我正在重用原型单元)。
谁能给我任何有关它为什么如此表现的见解,以及我该如何解决?
提前致谢!
编辑1:
这是我的cellForRowAtIndexPath
的完整代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set up the item
XYZItem *item = [self.itemsInView objectAtIndex:indexPath.row];
NSString *CellIdentifier = @"itemPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = item.itemName;
if ([self.selectedItem isEqual:indexPath])
{
cell.textLabel.numberOfLines = 2;
UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[newBtn setImage:[UIImage imageNamed:@"open.png"] forState:UIControlStateNormal];
[newBtn setFrame:CGRectMake(220, 0, 100, 86)];
[newBtn addTarget:self action:@selector(openImage:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:newBtn];
}
NSString *detailTextLabelText = [NSString stringWithFormat:@"%lu subitems", (unsigned long)[item.subItems count]];
cell.detailTextLabel.text = detailTextLabelText;
return cell;
}
该按钮显示在已重新使用先前添加按钮的单元格上。
这里有一些选择。
.hidden
属性时,将其设置为适当的值。 prepareForReuse
方法中的按钮。 假设self.tappedCell
是UITableViewCell
子类,而indexPath
是索引路径,则[self.tappedCell isEqual:indexPath]
将始终返回NO
我遇到同样的问题,并且我也有多个标题:
解决方案是:
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
headerLabel = [[UILabel alloc] initWithFrame:
CGRectMake(15, 5, self.tableView.frame.size.width, 15.0)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textAlignment = NSTextAlignmentLeft;
[headerLabel setFont:[UIFont fontWithName:@"Helvetica Neue Medium" size:13.0]];
headerLabel.tag = 1002;
[cell.contentView addSubview:headerLabel];
button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, HEIGHTROW)];
[button addTarget:self action:@selector(eventAddAlert) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:@"add_manually.png"] forState:UIControlStateNormal];
button.hidden = YES;
button.tag = 1001;
[cell.contentView addSubview:button];
}else{
// use viewWithTag to find lblNombre in the re-usable cell.contentView
headerLabel = (UILabel *)[cell.contentView viewWithTag:1002];
button = (UIButton *)[cell.contentView viewWithTag:1001];
}
if(indexPath.section == 0 && indexPath.row == 0){
button.hidden = NO;
}else{
button.hidden = YES;
.........
}
.........
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.