Brittany Brittany - 1 month ago 7
iOS Question

Can I execute didSelectRowAtIndexPath only for specific cell identifier?

I'm using two different custom table cells in my table view. That said, I only want didSelectRowAtIndexPath to fire when, for example, Custom Cell Identifier 2 is tapped (but not when Custom Cell Identifer 1 is tapped). How might I go about this? See code below. Right now, didSelectRowAtIndexPath fires when either cell is tapped...

- (void)viewDidLoad {
[super viewDidLoad];


static NSString *ChatTableIdentifier = @"ChatTableViewCell";
static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";


UINib *nib = [UINib nibWithNibName: ChatTableIdentifier bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier: ChatTableIdentifier];

UINib *nib2 = [UINib nibWithNibName: ChatTableIdentifier2 bundle:nil];
[self.tableView registerNib:nib2 forCellReuseIdentifier: ChatTableIdentifier2];

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *data = self.messages[indexPath.row];

id swaptime = data[@"swaptime"];
if ([swaptime isKindOfClass:[NSString class]]) {

static NSString *ChatTableIdentifier2 = @"SwapDetailTableViewCell";

SwapDetailTableViewCell *cell = (SwapDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier2 forIndexPath:indexPath];

NSString *time = data[@"swaptime"];
cell.startTime.text = time;

NSString *timeEnd = data[@"endswaptime"];
cell.endTime.text = timeEnd;

return cell;
} else {
static NSString *ChatTableIdentifier = @"ChatTableViewCell";

ChatTableViewCell *cell = (ChatTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ChatTableIdentifier forIndexPath:indexPath];

NSString *userName = data[@"name"];
cell.sendingUser.text = userName;

NSString *messageBody = data[@"body"];
cell.messageDisplayed.text = messageBody;

NSString *timeReceived = data[@"published at"];
cell.timeStamp.text = timeReceived;

return cell;
}
}


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


SwapDetailsViewController *detailViewController = [[SwapDetailsViewController alloc]
initWithNibName:@"SwapDetailsViewController" bundle:nil];

detailViewController.swapDetails = [self.messages objectAtIndex:indexPath.row];

[self presentViewController:detailViewController animated:YES completion:nil];

}

Answer

You can put an if condition inside didSelectRowAtIndexPath as the one you put in cellForRowAtIndexPath

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

        NSDictionary *data = self.messages[indexPath.row];

        id swaptime = data[@"swaptime"];

        //perform action based on this, and don't do anything in second case

        if ([swaptime isKindOfClass:[NSString class]])   

        //this indexPath will always contain cell of one kind

        }else{

         // this indexPath will contain second cell kind
        }
    }

Or alternatively you can also do-

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

       // this will give you cell at the selected indexPath
       id cell = [tableView cellForRowAtIndexPath:indexPath]

       //perform action based on this, and don't do anything in second case
       if ([cell isKindOfClass:[SwapDetailTableViewCell class]])                    

       }else{

       }
}
Comments