mmmtoastyxxx
mmmtoastyxxx

Reputation: 23

Being able to tap on ONLY one item in a UITableView

So I have a subclassed UITableView that lists data. I need to make only one cell selectable.

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// rows in section 0 should not be selectable
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if (indexPath.row == 3) {
    cell.userInteractionEnabled = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    NSLog(@"Hey whats up, address");
    return indexPath;
}
else {
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
   return nil;
}



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


[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

My code that does this so far works, but only after the cell is clicked at least once. Placing it in didSelectCell allows the cell to be selected if held down for 1-2 seconds.

Upvotes: 2

Views: 138

Answers (1)

Rui Peres
Rui Peres

Reputation: 25917

You should do that in the cellForRowAtIndexPath. Like this:

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

// your code
if(indexPath.row != 3)
{
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
{

}

And then on didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   if(indexPath.row == 3)
    {
        //do your stuff
    {

}

Upvotes: 3

Related Questions