1

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.

  • http://stackoverflow.com/questions/2728152/select-first-row-as-default-in-uitableview - try to use suggested code and implement your custom cell. – Himanshu padia Aug 04 '14 at 13:04
  • Do your custom cells actually show up? Because it looks like you're declaring `default` style cells and returning those. While you declare your custom cells and set some attributes on them, they never actually get returned. – Stonz2 Aug 04 '14 at 13:09
  • possible duplicate of [UITableView Multiple Selection](http://stackoverflow.com/questions/3040894/uitableview-multiple-selection); in particular, check out the answer by Raphael Oliveira – herzbube Aug 04 '14 at 13:46

1 Answers1

0

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];
}
Bharat
  • 2,987
  • 2
  • 32
  • 48