滚动

时间:2016-01-27 21:47:05

标签: ios objective-c uitableview

我试图在didSelectRowAtIndexPath方法上保持多个单元格的选定状态。我有一个编辑按钮,我已经设置了循环遍历每个单元格以选择我的UITableView上的每个字段。

以下是点击编辑按钮的代码,用于选择我的所有行。

- (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方法后弄清楚如何保持这些单元格的选定状态。欢迎任何建议。

3 个答案:

答案 0 :(得分:3)

我试图模拟你的情况,创建了一个customCell并在我的自定义selectedPaths可变数组中保存了selectedRows的索引路径(在viewDidLoad中初始化)。 每次单击后我删除或添加相关的索引路径到我的数组。 它适用于我的情况。希望它有所帮助。

-(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.
}

答案 1 :(得分:1)

您需要将单元格更新为选中状态,而不是cellForRowAtIndexPath中未明确选择双向

如果没有,再循环的细胞将只显示细胞最后一次使用的细胞的值,直到你改变它为止。

答案 2 :(得分: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中使用单元格选择状态,所以你可能也需要更改一些代码,但我不确定选择状态和你想要的关系是什么渲染细胞。

相关问题