【问题标题】:UITableView set multiple cell as selected?UITableView 将多个单元格设置为选中?
【发布时间】:2014-08-04 12:35:21
【问题描述】:

在我的表格视图中,我有两个部分,两个部分都有不同的自定义单元格。我想从每个部分中选择一行。可以吗? 这是我的方法 -

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

    UITableViewCell *cell;
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"categoryCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        ReportCategoryCell* catCell = (ReportCategoryCell *)cell;
        if (indexPath == selectedCategoryIndexPath)
        {
            catCell.selected = YES;
        }
        else
        {
            catCell.selected = NO;
        }
        catCell.categoryLabel.text = [categories objectAtIndex:indexPath.row];
    }
    else
    {
        static NSString *CellIdentifier = @"durationCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        ReportDurationCell* durationCell = (ReportDurationCell *)cell;
        if (indexPath == selectedDurationIndexPath) {

            durationCell.selected = YES;
        }
        else
        {
            durationCell.selected = NO;
        }
        durationCell.reportDurtionLabel.text = [durations objectAtIndex:indexPath.row];
    }

    cell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

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

    if (indexPath.section == 0) {
        selectedCategoryIndexPath = indexPath;
    }
    else
    {
        selectedDurationIndexPath = indexPath;
    }

}

但它只从任一部分中选择一行(我想从每个部分中选择一行)。我还需要默认选择每个部分的第一行。

【问题讨论】:

  • stackoverflow.com/questions/2728152/… - 尝试使用建议的代码并实现您的自定义单元格。
  • 您的自定义单元格真的出现了吗?因为看起来您正在声明 default 样式单元格并返回它们。当您声明自定义单元格并为其设置一些属性时,它们实际上从未被返回。
  • UITableView Multiple Selection 的可能重复项;特别是,请查看 Raphael Oliveira 的答案

标签: ios objective-c uitableview


【解决方案1】:

在自定义单元格的头文件中添加 bool 属性。并进行以下更改(假设您已适当初始化 selectedCategoryIndexPath 和 selectedDurationIndexPath) -

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

    UITableViewCell *cell;
    if (indexPath.section == 0) {

        static NSString *CellIdentifier = @"categoryCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        ReportCategoryCell* catCell = (ReportCategoryCell *)cell;
        if (indexPath.row == selectedCategoryIndexPath.row) {
        catCell.isSelected = YES;
        }
        else
        catCell.isSelected = NO;

        catCell.categoryLabel.text = [categories objectAtIndex:indexPath.row];
        catCell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
        catCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return catCell;
    }
    else
    {
        static NSString *CellIdentifier = @"durationCell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        ReportDurationCell* durationCell = (ReportDurationCell *)cell;
        if (indexPath.row == selectedDurationIndexPath.row) {
        durationCell.isSelected = YES;
        }
        else
        durationCell.isSelected = NO;

        durationCell.reportDurtionLabel.text = [durations objectAtIndex:indexPath.row];
        durationCell.backgroundColor = [UIColor colorWithRed:88/255.0 green:55/255.0 blue:175/255.0 alpha:1.0];
        durationCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return durationCell;
    }
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        selectedCategoryIndexPath = indexPath;
    }
    else
    {
        selectedDurationIndexPath = indexPath;
    }
     [self.tableView reloadData];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多