简体   繁体   中英

UITableView set multiple cell as selected?

In my table view I've two sections with different custom cell for both section.I want to select one row from each section.Is it possible? Here is my approach -

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

and

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

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

}

But it select only one row from either section(i want to select one row from each section).also i need first row of each section selected by default.

Add a bool property in your custom cell's header files. and make following changes (Assuming that you have initialised selectedCategoryIndexPath and selectedDurationIndexPath appropriately) -

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM