2016-10-26 7 views
0

私は、たいていチェックできる項目は3つまでです。UITableViewCellアクセサリのチェックマークは、行の選択後にスクロールに保存されません。

私はdidSelectRowAtIndexPath

selectedRowDictionaryはその後、私のcellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath]; 

if (_selectedRowDictionary && [_selectedRowDictionary count]) { 
    for (NSString *rowSelected in _selectedRowDictionary) { 
     BOOL isRowSelected = [[_selectedRowDictionary valueForKey:rowSelected] integerValue]; 
     if (isRowSelected){ 
      NSLog(@"rowSelected: %@", rowSelected); 
     } else { 
      NSLog(@"rowNotSelected: %@", rowSelected); 
     } 

     int rowIndexSelected = [[rowSelected substringFromIndex:[rowSelected length] - 1 ] integerValue]; 

     if (isRowSelected && rowIndexSelected == indexPath.row) { 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     } else { 
      cell.accessoryType = UITableViewCellAccessoryNone; 
     } 
    } 
}  
return cell; 
} 

で呼び出さNSMutabeDictionaryで選択した行を格納 - - didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 


if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    if (_selectedRowDictionary) { 
     [_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; 
     NSLog(@"row %d removed from array", indexPath.row); 
    } 

} else { 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [_selectedRowDictionary setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; 
} 

if ([_selectedRowDictionary count] > 3) { 
    UITableViewCell *lastSelectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    lastSelectedCell.accessoryType = UITableViewCellAccessoryNone; 

    [_selectedRowDictionary removeObjectForKey:[NSString stringWithFormat:@"row%d", indexPath.row]]; 
    NSLog(@"row selected > 3, row%d not selected", indexPath.row); 
} 
} 

私が引き起こしている何かが足りません私が下にスクロールしたときに再び選択解除されるセル彼はテーブルビューですか?

私は辞書をNSLog

が、それは私が、私はすでに、セルリサイクルの問題を解決したと思った、それらの行が存在し、私は同様の質問をチェックした

を選択されていることを言いますけど。

答えて

1

代わりに、可変配列を自由に使用できますか?

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath]; 

    if ([_selectedRowArray containsObject:indexPath]) 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    else 
     cell.accessoryType = UITableViewCellAccessoryNone; 

    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    if ([_selectedRowArray containsObject:indexPath]) { 
     [_selectedRowArray removeObject:indexPath]; 
    } else { 
     if (_selectedRowArray.count < 3) 
      [_selectedRowArray addObject:indexPath]; 
     else { 
      // Don't select it 
     } 
    } 

    [tableView reloadData]; 
} 
+0

「NSMutableArray」の表示されないインタフェースがあり、セレクタremoveObjectIndexPath'が宣言されていますか? 'removeObjectAtIndex:indexPath.row'を意味しましたか? – Simon

+0

私は結腸が欠けていました。私の答えを更新しました。 – norders

+0

ありがとう! – Simon

1

私はあなたのコードを編集しています、このコードを試してください。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifer" forIndexPath:indexPath]; 
    BOOL isRowSelected = [[_selectedRowDictionary valueForKey:@(indexPath.row)] boolValue] 
    if (isRowSelected) { 
     cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    } else { 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 
    return cell; 
} 

にコードを変更 - ユーザーが新しい行を選択するには、以前選択した行の選択を解除する必要がありますdidSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 


if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    if (_selectedRowDictionary) { 
     [_selectedRowDictionary removeObjectForKey:@(indexPath.row)]; 
     NSLog(@"row %d removed from array", indexPath.row); 
    } 

} else { 
    if (_selectedRowDictionary.count == 3) { 
     // Don't allow for Selection 
     return; 
    } 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    [_selectedRowDictionary setValue:@(YES) forKey:@(indexPath.row)]; 
    } 
} 

。私はあなたのコードを最適化して編集しました。私はこのコードがあなたに役立つことを願っています。

関連する問題