我想创建一个UITableView
与每个表格单元中的自定义UIButton
。我实现了这样的..UITableViewCell与自定义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
。
有人请给我建议来解决这个问题。
谢谢。
[self.radioButton setImage:[UIImage的imageNamed:@ “radio_blank.png”] forState:UIControlStateNormal]; [self.radioButton setImage:[UIImage imageNamed:@“radio_selected.png”] forState:UIControlStateSelected]; –
我没有看到任何问题: - # –
在你按下的按钮,我写了上面的代码。不在这里。该按钮处于选定状态,对于选定状态,图像为黑色 – Saad