HurkNburkS
HurkNburkS

Reputation: 5510

How to initialize a custom UITableViewCell from didSelectRowAtIndexPath

I would like to know how to get an instance of my custom UITableViewCell so that I can set a UITextfield as the first responder

This is how I set the custom cell inside cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomFinishingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

however when I try to set an instance of the selected cell inside didSelectRowAtIndexPath I get an error

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomFinishingCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // error
    [cell.widthTexField becomeFirstResponder];

This is the error I receive

Incompatible pointer types initializing 'CustomFinishingCell *' with an expression of type 'UITableViewCell *'

Upvotes: 1

Views: 230

Answers (3)

Mick MacCallum
Mick MacCallum

Reputation: 130193

You forgot to cast the UITableViewCell returned by cellForRowAtIndexPath: to your subclass

CustomFinishingCell *cell = (CustomFinishingCell *)[tableView cellForRowAtIndexPath:indexPath]; // error

Additionally, I don't know if you just omitted this from the code you've posted, but as is you never actually declared the cell in cellForRowAtIndexPath:

CustomFinishingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Upvotes: 3

johnMa
johnMa

Reputation: 3311

  1. initWithStyle is UITableViewCell's initialize method , did you override it and return CustomFinishingCell?

  2. Adding a cast

    CustomFinishingCell *cell = (CustomFinishingCell*)[tableView cellForRowAtIndexPath:indexPath]; 
    

Upvotes: 0

MAB
MAB

Reputation: 963

try adding a cast:

CustomFinishingCell *cell = (CustomFinishingCell*)[tableView cellForRowAtIndexPath:indexPath]; 

You should see a warning in this line saying that there is a problem in the affectation.

Upvotes: 0

Related Questions