Using some API, I am displaying data in table view format.
A) In that when API is called first time we will get 10 user details, so at first time we can see 10 rows in tableview. When we scroll down i.e after 10 rows, a new API called for nextPageURL i.e page 2 and it contains gain 10 user details. Again when you reach 20 row after again nextPageURL i.e page 3 API will call and again 10 records will get in JSON and again it will displayed in tableview. (This is working fine. There is no issue while getting data and displaying in data) This is the working flow my tableview in my project.
B) Here I am using UILongPressGestureRecognizer for selecting rows of tableview. Using UILongPressGestureRecognizer I can able to select multiple rows. (This is also working fine)
C) Code used for it, selecting and 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");
}
}
this is for first API call and here I will get 10 user details and I am displaying in tableview.
For getting next user details following method is called
-(void)loadMore
{
// next page API called here
}
for selecting row I am using following,
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 3;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
Selecting and deselecting rows
- (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];
}
}
My Problem/Issue -
I am selecting row of tableview using UILongPressGestureRecognizer, up to 10 rows (it is in front end) and in background in one array its id is storing. If you select some rows, its row id will add in selectedArray if you deselect row it will remove object from selectedArray
Now suppose I selected 5 tickets and suppose when I scroll down (after 10 rows) new API will call, and next 10 userdetails will display, but this time whatever selected rows are vanishing (selected rows are showing unselected) but still in background there id is stored.
What I want is that, when I select some rows even I scroll down and goes to any page, that selected arrow will not vanish and that that selected row is stored in selectedArray object
If you're using multiple selection, you can add this method to your ViewController and call it whenever you need to call [tableView reloadData] to preserve the selection.
- (void)reloadTableView
{
NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
[self.tableView reloadData];
for (NSIndexPath *path in indexPaths) {
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];
}
}
Referred from save selected row in UITableView after reloadData