Saturday, June 8, 2013

UITableView tableView didSelectRowAtIndexPath example in Objective C (iOS).


UITableView tableView didSelectRowAtIndexPath

Tells the delegate that the specified row is now selected.

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

Parameters of [UITableView tableView didSelectRowAtIndexPath]
tableView
A table-view object informing the delegate about the new row selection.
indexPath
An index path locating the new selected row in tableView.

Discussion of [UITableView tableView didSelectRowAtIndexPath]
The delegate handles selections in this method. One of the things it can do is exclusively assign the check-mark image (UITableViewCellAccessoryCheckmark) to one row in a section (radio-list style). This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode). See "€œ“Managing Selections”" in Table View Programming Guide for iOS for further information (and code examples) related to this method.

UITableView tableView didSelectRowAtIndexPath example.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;
    [myArray addObject:cellText];
}

Example of [UITableView tableView didSelectRowAtIndexPath].
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  DetailViewController *detail = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];

    UIView *headerView = (UIView *)[self tableView:tableView viewForHeaderInSection:indexPath.section];
    UILabel *dateLbl = (UILabel *) [headerView viewWithTag:1];

    UILabel *yrLbl = (UILabel *)[headerView viewWithTag:2];

    detail.titleStrng  = dateLbl.text;
    detail.TimeString  = yrLbl.text;

    [self.navigationController pushViewController:detail animated:YES];
}

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

    //This toggles the checkmark
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        //This sets the array
        [checkedIndexPaths replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:YES]];

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
         //This sets the array
        [checkedIndexPaths replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:NO]];

    }

}

End of UITableView tableView didSelectRowAtIndexPath example article.