【发布时间】:2012-01-16 07:30:30
【问题描述】:
我在我的 UITableViewCell 中使用了一个 UIButton,这个按钮可以被选中,金色星形和未选中,灰色星形,如下图所示。
问题是,当单元格被选中时,无论按钮是否被选中,它都会变成灰色(这个灰色不同于星形未选中模式的灰色)。我一直试图弄清楚为什么会发生这种情况,但没有运气。
这里是单元格的实现,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
//set the background of the cells
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"cellBackground.png"]];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellselected2.png"]];
// set the button content
[button setImage:[UIImage imageNamed:@"star.png" ] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"starSelected.png"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(selectedButton:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 10, 24, 24)];
[cell addSubview:button];
return cell;
}
当我在选择单元格时点击按钮时,按钮也会完全消失!
谢谢,
【问题讨论】:
标签: ios user-interface uitableview