2013-03-19 54 views
0

我正在使用UITableView自定義單元格(xib)。每個單元格都是lables和一個複選框(UIButton)。UITableViewCell複選框重複滾動

我在每個部分有2個部分和4個單元格。如果我檢查第一部分的第一個單元格,第二部分的第一個單元格也會被檢查,而我不想要。問題:dequeueReusableCellWithIdentifier:CellIdentifier。

我想保持我的單元格標識符是靜態的。

我該如何解決這個問題?

這是我的數組的初始化(對於我的單元的內容):

for(int i=0; i<NUMBER_OF_CELL; i++){ 

    Account *model = [[Account alloc]init]; 

    [model setAccountName:[NSString stringWithFormat:@"Account %d",i]]; 
    [model setAccountNumber:[NSString stringWithFormat:@"Number %d",i]]; 

    [_accountArray addObject:model]; 

} 

設置內容:

[[cell accountLabel] setText:_model.accountName]; 
[[cell accountNumberLabel] setText:_model.accountNumber]; 

編輯:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    _model = [_accountArray objectAtIndex:indexPath.row]; 

    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AccountCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 
    // configure cell 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    [[cell accountLabel] setText:_model.accountName]; 
    [[cell accountNumberLabel] setText:_model.accountNumber]; 
    // checkbox ? 

    if(cell.isChecked){ 

     NSLog(@"Checked"); 
    }else{ 
     NSLog(@"No checked"); 
    } 

    return cell; 
} 

在另一個c lass,我檢查複選框是否被選中:

- (IBAction)checkbox:(id)sender { 

    NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self]; 

    if(self.isChecked == NO) 
    { 
     self.isChecked = YES; 
     [_checkbox setImage:[UIImage imageNamed:@"checkbox_checked.png"] forState:UIControlStateNormal];   
    } 
    else{ 
     self.isChecked = NO; 
     [_checkbox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; 
    } 

} 

如何區分每個單元以避免重複檢查?

非常感謝! 此致敬禮,

+0

把代碼od cellforrowAtindexpath方法 – iPatel 2013-03-19 14:41:10

+0

完成;)謝謝:) – Lapinou 2013-03-19 14:46:28

回答

0

我認爲你最好使用選定的狀態爲按鈕來跟蹤它是否被檢查。看起來你已經創建了一個「AccountCell」類,所以你應該能夠簡單地用插座在AccountCell類的複選框,如:

@interface AccountCell : UITableViewCell 
... 
@property IBOutlet UIButton *checkBox; 

現在,你可以設定正常/檢查在AccountCell的viewDidLoad方法的按鈕圖像(因爲這並不需要做的每一次狀態的變化):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [_checkBox setImage:[UIImage imageNamed:"checkbox.png" forState:UIControlStateNormal]; 
    [_checkBox setImage:[UIImage imageNamed:"checkbox_checked.png" forState:UIControlStateSelected]; 
} 

在你cellForRowAtIndex路徑可以確保複選框通過使用返回的細胞清除:

cell.checkBox.selected = NO; 

然後在複選框的「行動」代碼,你可以使用以下方法來打開和關閉的複選框:

self.selected = !self.selected; 
+0

是的,我理解這個概念,但我不知道如何實現它:/ – Lapinou 2013-03-19 14:49:20

+0

編輯與更好的新的更好的答案,並應該解決您的問題 – cdemiris99 2013-03-19 15:29:25

1

你需要寫繼AccountCell.h/AccountCell.m

方法
- (void)resetCell { 

    //Reset Your cell content to default. So that you can reuse the cell. 
    NSIndexPath *indexPath = [(UITableView *)self.superview indexPathForCell: self]; 


     self.isChecked = NO; 
     [_checkbox setImage:[UIImage imageNamed:@"checkbox.png"] forState:UIControlStateNormal]; 

    //other resettings... 
} 

然後你就可以調用從(UITableViewCell的*)的tableView這種方法:如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    _model = [_accountArray objectAtIndex:indexPath.row]; 

    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AccountCell" owner:self options:nil]; 
     cell = [topLevelObjects objectAtIndex:0]; 
    } 
    // configure cell 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 

    //reset your cell's content. 
    [cell resetCell]; 

    //perform your task below 
    [[cell accountLabel] setText:_model.accountName]; 
    [[cell accountNumberLabel] setText:_model.accountNumber]; 
    // checkbox ? 

    if(cell.isChecked){ 

     NSLog(@"Checked"); 
    }else{ 
     NSLog(@"No checked"); 
    } 

    return cell; 
} 
(NSIndexPath *)indexPath:(UITableView的*)的tableView的cellForRowAtIndexPath

希望這有助於。

謝謝。

+0

是的,它可以工作,但是如果複選框被選中的單元格不可見(滾動後),則先前選中的複選框未被選中:/ – Lapinou 2013-03-19 15:11:46

+1

每當單元格離屏並返回到屏幕上時,我們將重置單元格的顯示resetCell(這是不包含任何值的單元格的默認顯示)。重置後,我們有責任將複選框標記爲已選中。我的意思是你必須維護數組中所有行的狀態(或者你正在使用的任何類型),並且當cellForRowAtIndexPath被調用時,你必須查看這個數組的狀態,並且以編程方式更新單元格的內容(在你的情況下,以編程方式檢查複選框) – Kunal 2013-03-19 15:22:42