【发布时间】:2012-12-19 11:29:46
【问题描述】:
我最近的挫败感是我的UITableView 的每个UITableViewCell 中的UIButton 子视图,根据每个indexPath 的特定条款,我想setHidden:。我的代码大致如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[self initCell:cell forIndexPath:indexPath];
}
[self updateCell:cell forIndexPath:indexPath];
return cell;
}
init 和 update 方法如下:
- (void)initCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
...
UIButton *btnMy = [UIButton buttonWithType:UIButtonTypeCustom];
btnMy.tag = kButtonMyTag;
[btnMy setFrame:CGRectMake(170, 45, 100, 30)];
[btnMy setBackgroundImage:[UIImage imageNamed:@"btn_image"] forState:UIControlStateNormal];
btnMy.adjustsImageWhenHighlighted = YES;
[btnMy setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnMy.titleLabel.font = [UIFont fontWithName:@"MyFont" size:14];
[btnMy addTarget:self action:@selector(btnMyPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btnMy];
UIImageView *imgViewAccessory = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"table_accessory"]];
cell.accessoryView = imgViewAccessory;
[imgViewAccessory release];
}
- (void)updateCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
UIButton *btnMy = (UIButton *)[cell viewWithTag:kButtonMyTag];
MyObject *object = (MyObject *)[self.dataSource objectAtIndex:indexPath.row];
if(object.meetsCondition)
{
btnMy.hidden = NO;
}
else
{
btnMy.hidden = YES;
}
...
}
令人沮丧的结果是,当滚动按钮时,按钮会随机显示和隐藏,而不是按照 updateCell 方法中的 if 子句预期的那样。 任何帮助将非常感激。提前致谢!
【问题讨论】:
-
将按钮添加到 cell.contentView 而不是 cell..
-
这也没有帮助。不过还是谢谢。
-
查看此链接中的答案..stackoverflow.com/questions/13877137/…
-
你为什么不做一个cutom cell?
-
在自定义单元实现中运行良好。我只想避免它,但只要它起作用,我就可以了。再次感谢大家!
标签: objective-c ios xcode