[IOS] The selected state of the cell is not lost after refreshing the tableview

1. Core statement:

[_tableView reloadData];
 [_tableView layoutIfNeeded];//Reset constraints

 After refreshing the table place, add:

   NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:_lastSelectRow];
    [_tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];

Because in my code, each line is a section, and a section has only one line, so pass in the value of indexPath.section in inSection. And because after reloadData, indexPath.section will be reset to 0, so just use a variable to record which section you are currently clicking on, for example:

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

    switch (indexPath.section) {
        case 0:
        {
            _lastSelectRow = indexPath.section;
            [self callNotificationReceiver:@"0"];
            break;
        }
。。。。。。。。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326096338&siteId=291194637