我正在尝试在每个表格单元格中创建UITableView
一个自定义项。UIButton
我是这样实现的..
@implementation CouponDetailsCustomTableViewCell
...............
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self setBackgroundColor:[UIColor whiteColor]];
CGRect frame = self.contentView.frame;
self.radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.radioButton setImage:[UIImage imageNamed:@"radio_blank.png"] forState:UIControlStateNormal];
[self.radioButton setImage:[UIImage imageNamed:@"radio_selected"] forState:UIControlStateSelected];
[self.radioButton setFrame:CGRectMake(16, 10, 29, 29)];
[self.radioButton addTarget:nil action:@selector(radioButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:radioButton];
}
@end
和 UITableView 委托一样......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *COUPON_CELL_ID = @"CouponCell" ;
CouponDetailsCustomTableViewCell * couponCell = (CouponDetailsCustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier:COUPON_CELL_ID];
if (couponCell == nil) {
couponCell = [[[CouponDetailsCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:COUPON_CELL_ID] autorelease];
couponCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[couponCell.radioButton setSelected:NO];
return couponCell;
}
并且 radioButtonPressed 方法是
-(void)radioButtonPressed:(id) sender
{
[sender setSelected:YES];
}
现在我运行程序并UIButton
在每个表格行中显示一个自定义。如果我单击一个按钮,它将被选中(显示radio_selected.png
)。
当我向下滚动表格时出现问题(我在窗口中只显示了 4 行表格)。当我再次向上滚动时..我看到的是单击的按钮正在显示radio_blank.png
。
我是 iPhone 开发的新手。我不知道为什么会这样。我能猜到的最多的是按钮属性正在改变 setSelected:NO
......
有人请给我建议来解决这个问题。
谢谢你。