19

I have a UITabBarController with more than 5 UITabBarItems so the moreNavigationController is available.

In my UITabBarController Delegate I do the following:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//do some stuff
//...

UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
    moreView.delegate = self;
}

I want to implement a UITableViewDelegate so I can capture the row that was selected, set a custom view property and then push the view controller:

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
  //how can I get the text of the cell here?
}

I need to get the text of a cell when the user taps on a row. How can I accomplish this?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

2 Answers2

52
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
      //how can I get the text of the cell here?
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      NSString *str = cell.textLabel.text;
}

A better Solution is to maintain Array of cell and use it directly here

    // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    Service *service = [self.nearMeArray objectAtIndex:indexPath.row];
    cell.textLabel.text = service.name;
    cell.detailTextLabel.text = service.description;
    if(![self.mutArray containsObject:cell])
          [self.mutArray insertObject:cell atIndex:indexPath.row];
    return cell;
}



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

    UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row];
    NSString *str = cell.textLabel.text;

}
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • 3
    Why is it better to maintain an array of the cell? – user592419 May 21 '13 at 20:01
  • if you call cellForRowAtIndexPath again just to capture the selected cell, It will unnecessary execute entire function and create cell again. Which would be performance costly compare to storing already created cell in the array – Mihir Mehta May 22 '13 at 04:20
  • If you use insertObject your array will be all over the place in no time. The docs say "If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.". So, when cellForRowAtIndexPath is called a second time (after scrolling maybe) the same cell is inserted but the original and all other cells are pushed forward in the array. – amergin Dec 05 '13 at 19:57
  • i have edited my answer. i hope this will resolve he problem. Thanks for bringing it to notice. – Mihir Mehta Dec 06 '13 at 22:40
  • 4
    A note/warning to others: `[tableView cellForRowAtIndexPath:...]` !== `[self tableView:tableView cellForRowAtIndexPath:...]`. In my tests, the latter returns a new instance of a cell each time, whereas the former will actually return the in-use instance if one exists. – devios1 Apr 23 '14 at 23:54
  • My table content is constantly being updated, so how would I get access to the array that originally drew the rows in this case? Doing the *easier* method gets the current text of the row at the time of the tap, so wouldn't that be a good exception to the rule, or is there still yet a better way that won't take such a performance hit? --Thanks – Jonathan Weinraub Mar 10 '19 at 22:29
0

Swift 5

You can always get your cell from UITableVIew or UICollectionView based on IndexPath like this:

tableView.cellForItem(at: indexPath)
collectionView.cellForItem(at: indexPath)
Đorđe Nilović
  • 3,600
  • 1
  • 25
  • 20