Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the important parts of the code needed for a grouped table. When any of the cells in a section are selected the first row changes color. Without initially setting the cellselectionstyle to none there is an annonying double reload when the user clicks row0 where the cell changes to bgColorView then fades and reloads bgColorView again. Good Luck and let me know if there is a simpler way to do this.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } if ([indexPath row] == 0) { cell.selectionStyle = UITableViewCellSelectionStyleNone; UIView *bgColorView = [[UIView alloc] init]; bgColorView.layer.cornerRadius = 7; bgColorView.layer.masksToBounds = YES; [bgColorView setBackgroundColor:[UIColor colorWithRed:.85 green:0 blue:0 alpha:1]]; [cell setSelectedBackgroundView:bgColorView]; UIColor *backColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1]; cell.backgroundColor = backColor; UIColor *foreColor = [UIColor colorWithWhite:1 alpha:1]; cell.textLabel.textColor = foreColor; cell.textLabel.text = @"row0"; } else if ([indexPath row] == 1) { cell.selectionStyle = UITableViewCellSelectionStyleNone; UIColor *backColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; cell.backgroundColor = backColor; UIColor *foreColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; cell.textLabel.textColor = foreColor; cell.textLabel.text = @"row1"; } else if ([indexPath row] == 2) { cell.selectionStyle = UITableViewCellSelectionStyleNone; UIColor *backColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; cell.backgroundColor = backColor; UIColor *foreColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; cell.textLabel.textColor = foreColor; cell.textLabel.text = @"row2"; } return cell; } #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:[indexPath section]]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]; [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; [tableView selectRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionNone]; } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tvStat cellForRowAtIndexPath:indexPath]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; } #pragma mark Table view Gestures -(IBAction)singleTapFrom:(UIGestureRecognizer *)tapRecog { CGPoint tapLoc = [tapRecog locationInView:tvStat]; NSIndexPath *tapPath = [tvStat indexPathForRowAtPoint:tapLoc]; NSIndexPath *seleRow = [tvStat indexPathForSelectedRow]; if([seleRow section] != [tapPath section]) [self tableView:tvStat didDeselectRowAtIndexPath:seleRow]; else if (seleRow == nil ) {} else if([seleRow section] == [tapPath section] || [seleRow length] != 0) return; if(!tapPath) [self.view endEditing:YES]; [self tableView:tvStat didSelectRowAtIndexPath:tapPath]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload