我想創建一個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