我試圖在didSelectRowAtIndexPath
方法上保留多個單元格的選定狀態。我有一個編輯按鈕,我已經設置,通過每個單元格循環來選擇我的UITableView上的每個字段。滾動時保持UITableViewCells的選定狀態
這是用於選擇我所有行的編輯按鈕的代碼。
- (IBAction)editButtonTapped:(id)sender {
for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) {
for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) {
[self tableView:caseDataTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:i]];
}
}
}
當調用didSelectRowAtIndexPath
方法,它執行以下操作的代碼。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
OKOperatieNoteTableViewCell *cell = (OKOperatieNoteTableViewCell *)[self.caseDataTableView cellForRowAtIndexPath:indexPath];
cell.cellIndexPath = indexPath;
[cell hideLabelAndShowButtons];}
因爲您在這裏想知道的是hideLabelAndShowButtons
方法。
- (void)hideLabelAndShowButtons {
self.caseDataKeyLabel.hidden = NO;
if (!self.disabled) {
self.caseDataValueLabel.hidden = YES;
self.textField.hidden = NO;
if ([self.inputType isEqualToString:@"switcher"] || [self.inputType isEqualToString:@"multiselect"] || [self.inputType isEqualToString:@"picker"] || [self.inputType isEqualToString:@"DatePicker"] || [self.inputType isEqualToString:@"selectContact"]) {
self.button.hidden = NO;
}else {
self.button.hidden = YES;
}
}
self.caseDataDescriptionTextView.hidden = YES;}
現在,在這一點上,我選擇了所有的行。如果我向下滾動然後備份,這些行的選擇不再存在。現在我知道,當你進入和退出視圖時,cellForRowAtIndexPath
方法重新創建這些單元格。以下是我的cellForRowAtIndexPath
方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"caseData";
OKOperatieNoteTableViewCell * cell = [[OKOperatieNoteTableViewCell alloc]init];
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (indexPath.row < _procedureVariables.count) {
if ([[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:@"key"] isEqualToString:@"Procedure"]) {
[cell setLabelsWithKey:[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:@"key"] AndValue:[self.model valueForKey:@"var_procedureName"]];
}else {
[cell setLabelsWithKey:[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:@"key"] AndValue:[[_caseDataArray objectAtIndex:indexPath.row] valueForKey:@"value"]];
}
OKProcedureTemplateVariablesModel *variableModel = _procedureVariables[indexPath.row];
cell.variable = variableModel.value;
[cell showLabelAndHideButtons];
cell.delegate = self;
[cell setUpCellType];
} else if (indexPath.row == _procedureVariables.count) {
NSString *text = [NSString stringWithFormat:@"%@ \n\n %@", [_templateDictionary objectForKey:@"indicationText"], [_templateDictionary objectForKey:@"procedureText"] ];
[cell showDescription:text];
NSLog(@"cell.caseDataDescriptionTextView.font.fontName = %@", cell.caseDataDescriptionTextView.font.fontName);
}
cell.procedureID = _procedureID;
[tableView setContentInset:UIEdgeInsetsMake(1.0, 0.0, 0.0, 0.0)];
return cell;
}
我只是試圖找出如何保持這些細胞的選中狀態,一旦cellForRowAtIndexPath
方法被調用。歡迎任何建議。
做你嘗試'self.tableView.allowsMultipleSelection = YES;' – meth
是的,我這樣做,我的viewDidLoad方法 self.caseDataTableView.allowsMultipleSelection = YES; self.caseDataTableView.allowsSelectionDuringEditing = YES; 我也有它在我的故事板上的視圖啓用。 – wowzuzz