Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS:即使我刷新tableview,如何保存tableview的选定行?_Ios_Objective C_Uitableview_Uilongpressgesturerecogni - Fatal编程技术网

iOS:即使我刷新tableview,如何保存tableview的选定行?

iOS:即使我刷新tableview,如何保存tableview的选定行?,ios,objective-c,uitableview,uilongpressgesturerecogni,Ios,Objective C,Uitableview,Uilongpressgesturerecogni,使用一些API,我以表视图格式显示数据 A)在第一次调用API时,我们将获得10个用户详细信息,因此第一次我们可以在tableview中看到10行。当我们向下滚动10行之后,一个新的API调用了nextPageURL,即第2页,它包含10个用户详细信息。同样,当您再次到达20行后,nextPageURL即第3页API将调用,10条记录将再次以JSON格式显示,并再次显示在tableview中(这工作正常。获取数据和在数据中显示时没有问题)这是我的项目中我的tableview的工作流程 B)这里我

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

A)在第一次调用API时,我们将获得10个用户详细信息,因此第一次我们可以在tableview中看到10行。当我们向下滚动10行之后,一个新的API调用了nextPageURL,即第2页,它包含10个用户详细信息。同样,当您再次到达20行后,nextPageURL即第3页API将调用,10条记录将再次以JSON格式显示,并再次显示在tableview中(这工作正常。获取数据和在数据中显示时没有问题)这是我的项目中我的tableview的工作流程

B)这里我使用ui长按手势识别器来选择tableview的行。使用UILongPressGestureRecognitor,我可以选择多行(这也可以正常工作)

C)用于它的代码,选择和取消选择tableview行

@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个用户详细信息,并在tableview中显示

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

-(void)loadMore
{

     // next page API called here
}
要选择我正在使用的行,请执行以下操作:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 3;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
选择和取消选择行

- (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];
    }

}
我的问题

我正在使用UILongPressGestureRecognitor选择tableview的行,最多10行(在前端),在一个阵列的后台,它的id正在存储。若选择某些行,其行id将添加到selectedArray中。若取消选择行,则将从selectedArray中删除对象

现在,假设我选择了5个票证,并假设当我向下滚动(在10行之后)时,将调用新的API,并将显示下10个userdetails,但这次无论选择的行消失(选择的行显示为未选择),但仍然在后台存储id


我想要的是,当我选择一些行时,即使我向下滚动并转到任何页面,选定的箭头也不会消失,并且选定的行存储在selectedArray对象中

如果使用多重选择,您可以将此方法添加到ViewController中,并在需要调用时调用它[tableView重新加载数据]以保留选择

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

参考

让我检查一下您的解决方案。非常感谢。它对我很有效。我从过去两天开始就在这个问题上陷入了困境,在您解决我的问题的一分钟内。现在,所选行不会消失,所选阵列也不会正确。