iOS : 如何在刷新表格视图时保存所选行的表格视图?

iOS : How to save selected row of tableview even I refresh tableview?

使用一些 API,我以 table 视图格式显示数据。

A) 因为第一次调用 API 时我们会得到 10 个用户详细信息,所以第一次我们可以在 [=66= 中看到 10 行]看法。当我们向下滚动时,即在 10 行之后,一个新的 API 调用 nextPageURL 即第 2 页,它包含获得 10 个用户详细信息。当您再次到达 20 行时,nextPageURL 即第 3 页 API 将再次调用,并且 10 条记录将进入 JSON 并再次显示在 table 视图中。 (这工作正常。获取数据和在数据中显示时没有问题)这是我在项目中的table视图的工作流程。

B) 这里我使用 UILongPressGestureRecognizer 用于 selecting 行 table 视图。使用 UILongPressGestureRecognizer 我可以 select 多行。 (这也很好用)

C) 用于它的代码,selecting 和 deselecting tableview row

@interface InboxViewController ()

{
    NSMutableArray *selectedArray;
    NSString *selectedIDs;
}

@property (strong,nonatomic) NSIndexPath *selectedPath;


- (void)viewDidLoad 
{

selectedArray = [[NSMutableArray alloc] init];
    self.tableView.allowsMultipleSelectionDuringEditing = true;
    UILongPressGestureRecognizer *lpGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(EditTableView:)];
    [lpGesture setMinimumPressDuration:1];
    [self.tableView addGestureRecognizer:lpGesture];

      [self reload]; // for getting data
}

-(void)reload
{

     // API sample
     NSString * url= [NSString stringWithFormat:@"%@api/v2/get-userDetails?token=%@&api=%@&show=%@&departments=%@",[userDefaults objectForKey:@"baseURL"],[userDefaults objectForKey:@"token"],apiValue,showInbox,Alldeparatments];
          NSLog(@"URL is : %@",url);


      // here get JSON (First 10 user details data)

}

-(void)EditTableView:(UIGestureRecognizer*)gesture{
    [self.tableView setEditing:YES animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.currentPage == self.totalPages
        || self.totalTickets == _mutableArray.count) {
        return _mutableArray.count;
    }


    return _mutableArray.count + 1;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

 if (indexPath.row == [_mutableArray count] - 1 ) {
        NSLog(@"nextURL111  %@",_nextPageUrl);

        if (( ![_nextPageUrl isEqual:[NSNull null]] ) && ( [_nextPageUrl length] != 0 )) {



            [self loadMore]; // this method is called for getting next data i.e getting next 10 user details


        }
        else{
              NSLog (@"ALL Caught UP");
            }

}

这是第一次 API 调用,在这里我将获得 10 个用户详细信息,我将在 table 视图中显示。

要获取下一个用户详细信息,请调用以下方法

-(void)loadMore
{

     // next page API called here
}

对于 select我正在使用的行,

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 3;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

选择和删除select行

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

    self.selectedPath = indexPath;

    if ([tableView isEditing]) {

        //  [selectedArray addObject:[_mutableArray objectAtIndex:indexPath.row]];

        [selectedArray addObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"id"]];

        count1=(int)[selectedArray count];
        NSLog(@"Selected count is :%i",count1);
        NSLog(@"Slected Array Id : %@",selectedArray);

        selectedIDs = [selectedArray componentsJoinedByString:@","];
         NSLog(@"Slected Ticket Id are : %@",selectedIDs);




    }else{

            // goes to next detail view
        }
}

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

    self.selectedPath = indexPath;


 //   [selectedArray removeObject:[_mutableArray objectAtIndex:indexPath.row]];
    [selectedArray removeObject:[[_mutableArray objectAtIndex:indexPath.row] valueForKey:@"id"]];

    count1=(int)[selectedArray count];
    NSLog(@"Selected count is :%i",count1);
    NSLog(@"Slected Id : %@",selectedArray);

    selectedIDs = [selectedArray componentsJoinedByString:@","];
    NSLog(@"Slected Ticket Id are : %@",selectedIDs);


    if (!selectedArray.count) {
        [self.tableView setEditing:NO animated:YES];
    }

}

我的Problem/Issue -

我正在使用 UILongPressGestureRecognizer select查看 table 视图的行,最多 10 行(它在前端)并且在一个数组中的后台存储它的 id。如果你 select 一些行,它的行 ID 将添加到 selectedArray 如果你 deselect 行它将从 selectedArray

中删除对象

现在假设我 select 编辑了 5 张票并假设当我向下滚动时(在 10 行之后)新的 API 将调用,接下来的 10 个用户详细信息将显示,但这次 selected 行正在消失(selected 行显示未selected)但仍在后台存储 id。

我想要的是,当我 select 一些行甚至我向下滚动并转到任何页面时,selected 箭头不会消失并且 selected 行存储在 selectedArray 对象

如果您使用多选,您可以将此方法添加到您的 ViewController 并在需要调用 [tableView reloadData] 以保留选择时调用它。

- (void)reloadTableView
{
    NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
    [self.tableView reloadData];
    for (NSIndexPath *path in indexPaths) {
        [self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

引用自save selected row in UITableView after reloadData