Saturday, June 8, 2013

UITableView tableView willDeselectRowAtIndexPath example in Objective C (iOS).


UITableView tableView willDeselectRowAtIndexPath

Tells the delegate that a specified row is about to be deselected.

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath

Parameters
tableView
A table-view object informing the delegate about the impending deselection.
indexPath
An index path locating the row in tableView to be deselected.

Return Value of [UITableView tableView willDeselectRowAtIndexPath]
An index-path object that confirms or alters the deselected row. Return an NSIndexPath object other than indexPath if you want another cell to be deselected. Return nil if you don’t want the row deselected.

Discussion of [UITableView tableView willDeselectRowAtIndexPath]
This method is only called if there is an existing selection when the user tries to select a different row. The delegate is sent this method for the previously selected row. You can use UITableViewCellSelectionStyleNone to disable the appearance of the cell highlight on touch-down.

UITableView tableView willDeselectRowAtIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)aTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSDictionary* friend = [friendsData objectAtIndex:[indexPath row]];
    if (![selectedIndexes containsObject:friend]) {
        [selectedIndexes addObject:friend];

    }
    else {
        [selectedIndexes removeObject:friend];
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        return nil;
    }
    return indexPath;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary* friend = [friendsData objectAtIndex:[indexPath row]];
    if (![selectedIndexes containsObject:friend]) {
         return indexPath;
     }
    // do not select so we can have multiple select
    return nil;
}

Example of [UITableView tableView willDeselectRowAtIndexPath].
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [tableView reloadData];
}

-(NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return indexPath;
}

UITableView tableView willDeselectRowAtIndexPath example.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  [tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor orangeColor];

  return indexPath;

}
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView cellForRowAtIndexPath:indexPath].textLabel.textColor = [UIColor whiteColor];

return indexPath;

}

End of UITableView tableView willDeselectRowAtIndexPath example article.