我在向tableview单元格添加按钮方面遇到了问题。基本上,我正在向特定单元格添加一个按钮,但该按钮也会出现在随机的其他单元格中(当前不在视图中)。
当我点击一个单元格时,我设置了一个变量(tappedCell=indexPath
),并重新加载该单元格。在我的cellForRowAtIndexPath
中,我检查indexPath
是否等于tappedCell
,如果是,我会添加一个按钮。这样做有效,但此按钮也出现在tableView
中更下方的其他单元格上。当我点击它时,这些单元格不在视图中,但是进一步向下,所以我必须滚动才能看到它们。如果我继续上下滚动(快速),会出现越来越多带按钮的单元格。这似乎是完全随机的。
我尝试在添加按钮的NSLog()
内添加if-statement
,但不会多次调用(当我点击原始单元格时)。所以它实际上并没有添加更多按钮,它必须与出现在多个单元格中的按钮相同。
我不知道为什么会这样。我尝试为每个单元格添加一个按钮,然后将hidden = YES
设置为标准,将hidden = NO
设置为[self.tappedCell isEqual:indexPath]
,但这不会改变任何内容......
在这里你可以看到我用来将按钮添加到我的单元格的代码:
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;
}
答案 0 :(得分:1)
该按钮显示在已重复使用之前添加的按钮的单元格上。
这里有一些选择。
.hidden
属性。prepareForReuse
方法中的按钮。假设self.tappedCell
是UITableViewCell
子类且indexPath
是索引路径,[self.tappedCell isEqual:indexPath]
将始终返回NO
答案 1 :(得分:0)
我遇到了同样的问题,我也有多个标题:
解决方案是:
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;
.........
}
.........