71

How can I get a UITableViewCell by indexPath? Like this:

I use the UIActionSheet, when I click confirm UIButton I want to change the cell text.

The nowIndex I define as a property

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSInteger section =  [nowIndex section];
        NSInteger row = [nowIndex row];
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
        formatter.dateFormat = @"yyyy-MM-dd";

        if (section == 0)
        {
            if (row == 0)
            {

            }
            else if (row == 1)
            {
                UILabel *content = (UILabel *)[[(UITableView *)[actionSheet ] cellForRowAtIndexPath:nowIndex] viewWithTag:contentTag];
                content.text = [formatter stringFromDate:checkInDate.date];
            }
            else if (row == 2)
            {

            }
        }
    }
}
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
phpxiaoxin
  • 1,043
  • 1
  • 9
  • 9

8 Answers8

118
[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex]

will give you uitableviewcell. But I am not sure what exactly you are asking for! Because you have this code and still you asking how to get uitableviewcell. Some more information will help to answer you :)

ADD: Here is an alternate syntax that achieves the same thing without the cast.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
Kent
  • 1,691
  • 4
  • 19
  • 27
Manjunath
  • 4,545
  • 2
  • 26
  • 31
  • 1
    *Technically* you want to call that on whatever the `dataSource` property of your tableView is set to, but usually that is `self`. – devios1 May 08 '13 at 21:36
27

Here is a code to get custom cell from index path

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
 YourCell *cell = (YourCell *)[tblRegister cellForRowAtIndexPath:indexPath];

For Swift

let indexpath = NSIndexPath(forRow: 2, inSection: 0)
let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! CellTest

For Swift 4 (for collectionview)

let indexpath = NSIndexPath(row: 2, section: 0)
let cell = self.colVw!.cellForItem(at: indexpath as IndexPath) as? ColViewCell
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
  • In the case of having two custom cell and I need to know which type of cell are returned on this index path. What would be the best approach to get the type? –  Jun 26 '17 at 18:04
26

Finally, I get the cell using the following code:

UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex];

Because the class is extended UITableViewController:

@interface SearchHotelViewController : UITableViewController

So, the self is "SearchHotelViewController".

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
phpxiaoxin
  • 1,043
  • 1
  • 9
  • 9
13

For Swift 4

    let indexPath = IndexPath(row: 0, section: 0)
    let cell = tableView.cellForRow(at: indexPath)
Celil Bozkurt
  • 1,693
  • 16
  • 18
7

You can use the following code to get last cell.

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];
Hardik Mamtora
  • 1,642
  • 17
  • 23
6

I'm not quite sure what your problem is, but you need to specify the parameter name like so.

-(void) changeCellText:(NSIndexPath *) nowIndex{
    UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] contentView] viewWithTag:contentTag];
    content.text = [formatter stringFromDate:checkInDate.date];
}
David Kanarek
  • 12,611
  • 5
  • 45
  • 62
4

Swift 3

let indexpath = NSIndexPath(row: 0, section: 0)
let currentCell = tableView.cellForRow(at: indexpath as IndexPath)!
Oscar Falmer
  • 1,771
  • 1
  • 24
  • 38
4

Swift

let indexpath = IndexPath(row: 0, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? <UITableViewCell or CustomCell> {
    cell.backgroundColor = UIColor.red
}
Krunal
  • 77,632
  • 48
  • 245
  • 261