【发布时间】:2010-04-13 22:18:30
【问题描述】:
我有一个表,我有它的 cellForRowAtIndexPath 委托,并且我在该方法中创建了 UITableViewCell 的实例,我将其返回。在 cellForRowAtIndexPath 的某个地方,我还将 UIButton 添加到该单元格的 cell.contentView 中。一切正常,直到我选择带有按钮的单元格。单元格将颜色更改为蓝色(没关系), uibutton 也将其颜色更改为蓝色。现在,如果我单击选定单元格内的 UIButton,它就会消失!哇,好奇怪,怎么解决?当我单击它时,我希望选定单元格内的 UIButton 保持不变。
编辑:包括我的 cellForRowAtIndexPath 实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *sectionsTableIdentifier = @" sectionsTableIdentifier ";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: sectionsTableIdentifier];
for (UIView *view in cell.contentView.subviews)
[view removeFromSuperview];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: sectionsTableIdentifier] autorelease];
}
CGRect newIconRect = CGRectMake(276, 40, 29, 29);
UIButton *newIcon = [[UIButton alloc] initWithFrame:newIconRect];
[newIcon setImage:[UIImage imageNamed:@"mynewicon.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:newIcon];
[newIcon release];
return cell;
}
【问题讨论】:
-
我们可以看看你的
cellForRowAtIndexPath方法的内容吗?tableView:didSelectRowAtIndexPath:也会很有帮助,如果你已经实现了。 -
是的,我在编辑中包含了它,我没有实现 didSelectRowAtIndexPath,只有 cellForRowAtIndexPath
-
当您说按钮“消失”时,它是隐藏起来了,还是实际上不复存在?如果你按下它应该在的区域,按钮的点击处理程序会被调用吗?
-
它变成“隐藏”,如果我再点击几次,按钮会再次出现,甚至颜色从蓝色(选中)变为正常。
-
我也有 willSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法,只是改变了一些与表格无关的变量,最后返回 indexPath。
标签: iphone uitableview uibutton