私は、たいていチェックできる項目は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
「NSMutableArray」の表示されないインタフェースがあり、セレクタremoveObjectIndexPath'が宣言されていますか? 'removeObjectAtIndex:indexPath.row'を意味しましたか? – Simon
私は結腸が欠けていました。私の答えを更新しました。 – norders
ありがとう! – Simon