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

时间:2017-11-30 08:38:01

标签: ios objective-c uitableview uilongpressgesturerecogni

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

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

B)我在这里使用 UILongPressGestureRecognizer 来选择tableview行。使用UILongPressGestureRecognizer我可以选择多行。 (这也正常)

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

}

我的问题/问题 -

我正在使用UILongPressGestureRecognizer选择一行tableview,最多10行(它在前端),在背景中选择其id存储的一个数组。如果选择某些行,如果取消选择行,它的行ID将添加到selectedArray中,它将从selectedArray中删除对象

现在假设我选择了5张票,并且假设当我向下滚动(10行之后)新API将调用,接下来将显示10个用户详细信息,但这次所选行消失(所选行显示未选中)但仍在背景那里存有id。

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

1 个答案:

答案 0 :(得分:1)

如果您正在使用多个选择,则可以将此方法添加到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];
    }
}

摘自refs

相关问题