【发布时间】:2013-07-07 11:55:45
【问题描述】:
我创建了一个自定义单元格并在其上添加了一个 uibutton。在点击该按钮时,我设置该按钮选择了更改按钮的图像。
-(IBAction)btnInfoPressed:(id)sender
{
[btnInfo setSelected:YES];
}
上述方法在自定义单元格类中。现在,当我向下滚动时,在某些单元格之后,即使我没有点击该按钮,也会选择其他单元格的按钮。
这是我的 cellforrowatindexpath 方法:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CustomCell";
CustomCell *c = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (c == nil)
{
c = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
}
c.selectionStyle = UITableViewCellSelectionStyleNone;
return c;
}
有什么想法需要做些什么吗?
【问题讨论】:
-
显示方法
[btnInfo setSelected:YES]. -
它是默认的 UIButton 属性,用于将其状态从正常更改为选中或恢复正常。
-
那你不应该用
[sender setSelected:YES]吗? -
它仍然使其他单元格中的按钮被选中。
-
@AmmadHussain:但原因是一样的。表格视图仅分配有限数量的单元格。如果您向下滚动,
dequeueReusableCellWithIdentifier将返回已变为不可见的现有单元格之一。因此,您必须在 cellForRowAtIndexPath 中更新单元格的完整状态(包括按钮的状态)。
标签: iphone ios uitableview custom-cell reuseidentifier