简体   繁体   中英

In UiTableView didSelectRowAtIndexPath updating wrong Cell

In my TableView didSelectRowAtIndexPath is updating only the last visible cell of table on every cell is selection.

Here is the sample code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:subcategoryTV])
    {
         selCell=(MDTableViewCell *)[subcategoryTV cellForRowAtIndexPath:indexPath ];

         if ([checkButton.titleLabel.text isEqualToString:@"✓"])
         {
            checkButton.backgroundColor=[UIColor clearColor] ;
            [checkButton setTitle:resetTitle forState:UIControlStateNormal];
            [checkButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
            checkButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        }
        else
        {
            resetTitle=checkButton.titleLabel.text;
            NSLog(@"%@",resetTitle);

            checkButton.backgroundColor=[UIColor colorWithRed:1 green:0.839 blue:0.314 alpha:1] ;
            [checkButton setTitle:@"✓" forState:UIControlStateNormal];
            [checkButton.titleLabel setFont:[UIFont fontWithName:@"Futura" size:25]];
            [checkButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            checkButton.layer.borderColor = [UIColor clearColor].CGColor; 
        }

        if (costLabel.hidden==YES) {
            costLabel.hidden=NO;
        }
        else
        {
            costLabel.hidden=YES;
        }
     }
 }

You should not be querying data from the cell anyway, you should be checking your model to decide what to do. When any rows or buttons are changed by the user you update the model and then use that to update the view. This is the correct approach to take.

Your specific problem here, with your bad approach, is that you get the cell at the selected index path and then don't use it. You should get the button and label from the cell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM