UITableView将多个单元格设置为选中状态?

时间:2014-08-04 12:35:21

标签: ios objective-c uitableview

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

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

}

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

1 个答案:

答案 0 :(得分:0)

在自定义单元格的头文件中添加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];
}