2013-12-10 61 views
1

我選擇在我UITableViewCell一個UIButton和保持它選擇的Ive加入一個圖像每UIButton的每個狀態(通過Interface Builder,不編程)。但是當我滾動時,選中的狀態顯示爲重複。我讀了很多類似的信息,但無法找到解決我的問題的帖子。我注意到tableView正在重複使用緩存中的單元格,其狀態爲UIButton重複的的UIButton的選擇狀態中的UITableViewCell插入時滾動的UITableView

這是我的代碼。

以我Custom Cell

- (IBAction)highlightStar:(UIButton *)sender { 
    self.starButton.selected=!sender.selected; 
} 

以我控制器viewDidLoad

UINib *nib = [UINib nibWithNibName:@"CellS" 
           bundle:nil]; 

    [self.tableView registerNib:nib 
    forCellReuseIdentifier:CellIdentifier]; 

以我控制器cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    CellS *cell = (CellS *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 



    cell.backgroundColor = [UIColor clearColor]; 

    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 

    NSDictionary *dict=self.items[indexPath.row]; 

    NSString *header=[dict objectForKey:@"header"]; 

    [cell.labelReader setText:header]; 



    return cell; 
} 

捕捉1(不滾動)。選擇的兩個UIButtons:

enter image description here

捕獲2(與滾動)。選擇了兩個重複的UIButton:

enter image description here

感謝

回答

0

我找到了解決方案!改變了我的模型,並且每次更改最喜歡的狀態時,我都會更改模型,並更新模型中的單元格。

cellForRotAtIndexPath

Model *new=nil; 
//I parse the plist, and fill an array with Model Objects, where each Object is an item from the Plist. The method newsAtIndex returns the Model Object for the selected index 
new=[self.model newsAtIndex:indexPath.row]; 
cell.labelReader.text=new.header; 
[cell.starButton addTarget:self action:@selector(didTapStarButton:)  forControlEvents:UIControlEventTouchUpInside]; 
cell.starButton.tag = indexPath.row; 
cell.starButton.selected=new.Favorite; 

而且在didTapStarButton

Model *new=nil; 
new=[self.model newsAtIndex:button.tag]; 
new.Favorite=button.selected; 
0

由於細胞滾動,你也需要你從cellForRowAtIndexPath

+0

謝謝!我正在努力, – santibernaldo

0

返回之前在你Custom Cell類更新按鈕狀態中得到重用,添加以下:

- (void)prepareForReuse 
{ 
    self.starButton.selected= NO; 
} 

同樣在你的cellForRowAtIndexPath中,你應該更新starButton狀態:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    SBQCell *cell = (SBQCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell.backgroundColor = [UIColor clearColor]; 

    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 

    NSDictionary *dict=self.items[indexPath.row]; 

    NSString *header=[dict objectForKey:@"header"]; 

    [cell.labelReader setText:header]; 

    // HERE - Update cell's starButton state according to your current data info 
    cell.starButton.selected = [dict objectForKey:@"selectedState"]; 

    // Keep track of indexPath.row and add target 
    cell.starButton.tag = indexPath.row; 
    [cell.starButton addTarget:self action:@selector(didTapStarButton:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

應該這樣做。

編輯:我添加了一種方法來跟蹤您的starButton在cellForRowAtIndexPath中的狀態。您需要添加在你的viewController如下:

- (void)didTapStarButton:(UIButton *)button 
{ 
    button.selected = !button.selected; 
    // Here, keep track of the change in state with button.tag that is equal to the cell's indexPath.row 

} 

您可以刪除改變您的自定義單元格的starButton的地位,因爲你現在要做的是這裏的代碼。

+0

謝謝隊友!我快速去嘗試一下並告訴你一些信息 – santibernaldo

+0

但是我怎樣才能得到我從Custom Cell類中觸及的那一行的indexPath?我試着用CGPoint buttonPosition = [發件人convertPoint:CGPointZero toView:self.tableView]; NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition] ;.我使用Controller中創建的協議方法獲取self.tableView,但它沒有收到數據。我在init方法中設置委託。 – santibernaldo

+0

我必須更改自定義單元格中的數據模型,不是嗎?它改變了UIButtons的狀態。但是我怎樣才能將BOOL狀態與自定義單元格中的ROW聯繫起來呢? – santibernaldo

相關問題