How can I get a uitableViewCell by indexPath?

IphoneUitableviewNsindexpath

Iphone Problem Overview


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)
            {
    
            }
        }
    }
}

Iphone Solutions


Solution 1 - Iphone

[(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];

Solution 2 - Iphone

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".

Solution 3 - Iphone

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

Solution 4 - Iphone

For Swift 4

    let indexPath = IndexPath(row: 0, section: 0)
    let cell = tableView.cellForRow(at: indexPath)

Solution 5 - Iphone

You can use the following code to get last cell.

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];

Solution 6 - Iphone

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];
}

Solution 7 - Iphone

Swift 3

let indexpath = NSIndexPath(row: 0, section: 0)
let currentCell = tableView.cellForRow(at: indexpath as IndexPath)!

Solution 8 - Iphone

Swift

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionphpxiaoxinView Question on Stackoverflow
Solution 1 - IphoneManjunathView Answer on Stackoverflow
Solution 2 - IphonephpxiaoxinView Answer on Stackoverflow
Solution 3 - IphoneHardik ThakkarView Answer on Stackoverflow
Solution 4 - IphoneCelil BozkurtView Answer on Stackoverflow
Solution 5 - IphoneHardik MamtoraView Answer on Stackoverflow
Solution 6 - IphoneDavid KanarekView Answer on Stackoverflow
Solution 7 - IphoneOscar FalmerView Answer on Stackoverflow
Solution 8 - IphoneKrunalView Answer on Stackoverflow