2016-01-27 24 views
0

我試圖在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方法被調用。歡迎任何建議。

+0

做你嘗試'self.tableView.allowsMultipleSelection = YES;' – meth

+0

是的,我這樣做,我的viewDidLoad方法 self.caseDataTableView.allowsMultipleSelection = YES; self.caseDataTableView.allowsSelectionDuringEditing = YES; 我也有它在我的故事板上的視圖啓用。 – wowzuzz

回答

3

我試圖模仿你的情況,創造了customCell和我的自定義selectedPaths可變數組(在viewDidLoad初始化)保存selectedRows的indexpaths。 每次點擊後,我刪除或添加相關的索引路徑到我的數組。 它適用於我的情況。希望能幫助到你。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"caseData"; 
    NOTableViewCell *cell = (NOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    { 
     NSLog(@"new cell created for row %d", (int)indexPath.row); 
     cell = [[[NSBundle mainBundle] loadNibNamed:@"NOTableViewCell" owner:self options:nil] objectAtIndex:0]; 
    } 
    if ([selectedPaths indexOfObject:indexPath] != NSNotFound) // this cell is in selected state. 
    { 
     [cell.textLabel setText:@"This cell selected"];//selected state job. 
     return cell; 
    } 
     [cell.textLabel setText:[NSString stringWithFormat:@"%d", (int)indexPath.row]]; 
    return cell; 
} 
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if ([selectedPaths indexOfObject:indexPath] != NSNotFound) { 
     [selectedPaths removeObject:indexPath]; 
    } 
    else{ 
     [selectedPaths addObject:indexPath]; 
    } 
    //[tableView reloadData]; 

    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];//instead of reloading all just reload clicked cell. 
} 
+0

你從哪裏得到selectedPaths? – wowzuzz

+0

我創建了它,它最初是空白的。當用戶選擇行時,我添加相關的索引路徑或刪除,如果它已被添加。 – meth

+0

我使用未聲明的標識selectedPaths。 – wowzuzz

1

您需要將單元格更新爲選中且未明確選中兩個方向,cellForRowAtIndexPath

如果不是,再循環的細胞將顯示細胞上次使用的細胞的值,直到您更改爲止。

+0

我該怎麼做? cell.selected = YES? 當你說兩個方向,你能澄清? – wowzuzz

+0

我認爲你正在使用類似'[cell hideLabelAndShowButtons]'的東西。你也需要相反的。但你當然可以使用'UITableViewCell'' setSelected:animated'方法(並且如果你的子類需要覆蓋它的話)。 – Mundi

+0

這兩個方向意味着你應該在'cellForRowAtIndexPath'中顯式地設置選定和非選擇狀態,如下所示:'if(_yourObject.needToBeSelected){cell.selected = YES;} else {cell.selected = NO;}' –

0

當您調用委託方法以調用hideLabelAndShowButtons時,您並未告訴表視圖您已選擇該行;

- (IBAction)editButtonTapped:(id)sender { 

    for (int i = 0; i < self.caseDataTableView.numberOfSections; i++) { 
     for (NSInteger r = 0; r < [self.caseDataTableView numberOfRowsInSection:i]; r++) { 
      NSIndexPath *path=[NSIndexPath indexPathForRow:r inSection:i]; 
      [caseDataTableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone]; 
      [self tableView:caseDataTableView didSelectRowAtIndexPath:path]; 
     } 
    } 
} 

此外,不使用小區選擇狀態cellForRowAtIndexPath,所以你可能需要在那裏改變一些代碼太多,但我不知道的關係是什麼選擇狀態之間,你想如何渲染細胞。

相關問題