2011-12-31 30 views
0

テーブルビューで複数の選択を有効にする必要があります。また、選択したもの(配列などに保存するもの)を追跡する必要があります。これまでの私のアプローチ。 テーブルから複数のレコードを選択する

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    cell.textLabel.text=[arrayobject objectAtIndex:indexPath.row]; 

    bool xx = [[allmyselectedobjects objectAtIndex:indexPath.row] containsIndex:1]; 

     if (xx) { 
      [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
     }else{ 
      [cell setAccessoryType:UITableViewCellAccessoryNone]; 
     } 

    return cell; 
} 

とdidSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self.cuisineTableView cellForRowAtIndexPath:indexPath]; 
    if ([cell accessoryType] == UITableViewCellAccessoryNone) { 
     [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; 
     [self.allmyselectedobjects insertObject:1 atIndex:indexPath.row];   
    } 
    else { 
     [cell setAccessoryType:UITableViewCellAccessoryNone]; 
     [self.allmyselectedobjects insertObject:0 atIndex:indexPath.row]; 
    } 
} 

私は複数のレコードをクリックすることもできますが、私はスクロールダウンしたときに、私も(私が選択していない)のチェックマークでチェックさ他のセルを参照してください。私はこれを数日間試していますが、誰かがこのコードを修正するのを助けることができますか?

答えて

0

特定のセルインスタンスがリサイクルされ、多くの行に使用されます。あなたのチェックしたindexPathesを保存しますが、セルは保存しないでください。

+0

これは解決策になるかもしれません。 – Illep

+0

上記のようにNSIndexSetを使用して、選択したセルの行インデックスを格納します。 –

+0

をNSIndexSetの意味で使うと、allmyselectedobjectsの型をNSMutableArrayではなくNSIndexSetに変更することを意味しましたか? – Illep

0

Bool xx = 

前にこれを追加します。あなたがチェックした細胞を保存するためにNSIndexSetを使用しているようだ

 [cell setAccessoryType:UITableViewCellAccessoryNone]; 
0

。したがって、選択したセルのインデックスを挿入してテストするだけです。

// Test 
BOOL xx = [allmyselectedobjects containsIndex: indexPath.row] 

// Selected cell 
[allmyselectedobjects addIndex: indexPath.row] 

// Unselected cell 
[allmyselectedobjects removeIndex: indexPath.row] 
+0

いいえ.. allmyselectedobjectsは、私がチェックされたセルを格納するために使用しているNSMutableArrayです – Illep

+0

以下の答えです。 –

関連する問題