Saturday, June 8, 2013

UITableView tableView shouldHighlightRowAtIndexPath example in Objective C (iOS).


UITableView tableView shouldHighlightRowAtIndexPath

Asks the delegate if the specified row should be highlighted.

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath

Parameters of [UITableView tableView shouldHighlightRowAtIndexPath]
tableView
The table-view object that is making this request.
indexPath
The index path of the row being highlighted.

Return Value
YES if the row should be highlighted or NO if it should not.

Discussion of [UITableView tableView shouldHighlightRowAtIndexPath]
As touch events arrive, the table view highlights rows in anticipation of the user selecting them. As it processes those touch events, the table view calls this method to ask your delegate if a given cell should be highlighted. Your delegate can implement this method and use it to prevent the highlighting of a row when another row is already selected or when other relevant criteria occur.

If you do not implement this method, the default return value is YES.

UITableView tableView shouldHighlightRowAtIndexPath example.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
   return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // do something here
}

Example of [UITableView tableView shouldHighlightRowAtIndexPath].
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}

- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // Add your Colour.
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [self setCellColor:[UIColor whiteColor] ForCell:cell];  //highlight colour
}

- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath {
  // Reset Colour.
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath];
    [self setCellColor:[UIColor colorWithWhite:0.961 alpha:1.000] ForCell:cell]; //normal color

}

- (void)setCellColor:(UIColor *)color ForCell:(UITableViewCell *)cell {
    cell.contentView.backgroundColor = color;
    cell.backgroundColor = color;
}

UITableView tableView shouldHighlightRowAtIndexPath example.
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.height;
}

- (BOOL)tableView:(UITableView*)tableView shouldHighlightRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.selectable;
}

- (NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyCellInfo* cellInfo = dataSource[indexPath.section];

    return cellInfo.selectable ? indexPath : nil;
}

End of UITableView tableView shouldHighlightRowAtIndexPath example article.