2012-05-16 137 views
1

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

有人請給我建議來解決這個問題。

謝謝。

回答

2

當你滾動你的UITableView時,隱藏的單元格不再被渲染,並且可能被重用於變得可見的單元格。如果新單元格變得可見,tableView:cellForRowAtIndexPath:被調用。

的問題是,你設置選擇狀態有:

[couponCell.radioButton setSelected:NO]; 

因此,當您滾動你的出來的可見區域,然後再返回,它就會被重置爲selected = NO

我建議你創建一個NSMutableDictionary,您可以在其中存儲每行/ NSIndexPath的選擇狀態,然後在tableView:cellForRowAtIndexPath:中重新應用該選擇狀態。

0
-(void)radioButtonPressed:(id) sender 
{ 
    [sender setSelected:YES]; 
} 
在此方法中要設置按鈕

的選擇和選擇按鈕,您可以設置radio_blank.png圖像

+0

[self.radioButton setImage:[UIImage的imageNamed:@ 「radio_blank.png」] forState:UIControlStateNormal]; [self.radioButton setImage:[UIImage imageNamed:@「radio_selected.png」] forState:UIControlStateSelected]; –

+0

我沒有看到任何問題: - # –

+0

在你按下的按鈕,我寫了上面的代碼。不在這裏。該按鈕處於選定狀態,對於選定狀態,圖像爲黑色 – Saad

2

tableView:cellForRowAtIndexPath:與代碼替換[couponCell.radioButton setSelected:NO];,設置選定的屬性取決於您的數據源的狀態。

類似的規定:在移動時關閉屏幕

/* get the button state from your data source */ 
FancyCouponObject *coupon = [self.coupons objectAtIndex:indexPath.row]; 
BOOL buttonState = coupon.buttonState; 
[couponCell.radioButton setSelected:buttonState]; 

一個的tableView的細胞被重新使用。你不能保存狀態。

1

問題是,當你在那個打電話的時候每一行的cellForRowAtIndexPath:委託方法滾動表...所以這裏的時候,它在時間叫你與像波紋管NO參數setSelected方法調用...

[couponCell 。radioButton setSelected:NO];

,所以當你在一次滾動表您的setSelected方法調用和你的按鈕依次radio_blank.png ... :)