简体   繁体   中英

Reload UITableView data and scroll down to last selected cell

I have a long list of data to show in a UITableView . One cell of the table contains many things like comments, share and like. Now when user likes a post in the cell it reloadTableData and move to the first one again.

For example user have scrolled down to 30 cells and then like 29th cell, it'll reload the data and goes to the top again.

So how to make it stays at the same place after reloading the data? Here is my code:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
        return imagesArr.count;
}

And for the implmemtation of Like button

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HomeCell *cellObj = (HomeCell*)[tableView dequeueReusableCellWithIdentifier:@"HomeCell" ];
cellObj.likeBtn.tag=indexPath.row;
[cellObj.likeBtn addTarget:self action:@selector(loveButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
return cellObj;
    }

loveButtonClicked button clicked.

-(void)loveButtonClicked
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:nil, nil];
    [[RRDataAcessLayer sharedDataAccessLayerManager]sendRequestToFetchHomeData:dict];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeAnnularDeterminate;
   // hud.labelText = @"Loading Data";

}

Image below will give you an idea. two cells are given and when the heart icon is tapped it reloads to update the value of total love. 应用图片

  • Saved edited index in variable
  • Get indexpath for that index
  • Scroll to table after reoladdata to that index
 NSIndexPath* ip = [NSIndexPath indexPathForRow:editedRowNumber inSection:0]; [self.tableViewTimeline scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
  • If your table view has one section you can pass zero if has number of section you have to pass section in which user edit cell

in like button action you can reload cell without scrolling by using both ways while best just reload selected index as suggest @Mhesh

    int selectedIndex=(int) btn.tag;
    int likes = [[self.arrayDataSource objectAtIndex:selectedIndex] intValue]+1;
    [self.arrayDataSource replaceObjectAtIndex:selectedIndex withObject:[NSString stringWithFormat:@"%d",likes]];
    NSIndexPath* ip = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
    /*
    [self.refWeakTableView reloadData];
    [self.refWeakTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionNone animated:YES];
     */
    [self.refWeakTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:ip]withRowAnimation:UITableViewRowAnimationNone];

and cell for row method set like button tag

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MyTableViewCell";

    myTableViewCell *cell = (myTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"myTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    //    DataSourceModel* dbObject = (DataSourceModel*)[self.arrayDataSource objectAtIndex:indexPath.row];
    cell.myCellLabel.text = [NSString stringWithFormat:@"%@",self.arrayDataSource[indexPath.row]];
    int indexed = (int)indexPath.row;

    cell.btnLike.tag = indexed;


    return cell;
}

If action is like affected to one cell then why you reload the whole table, reload the only single cell using below code

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; 

You can change UITableViewRowAnimation .

I'd recommend to cache the current selection in didSelectedCellAtIndexPath in your Controller and if you reload the whole table you could scroll to this last cached indexPath with the tableView's methods for scrolling. :)

You should not reload all table data, but only the rows you want. You can do this by calling this method on tableView reloadRowsAtIndexPaths(_:withRowAnimation:)
It will reload only desired cells with a desired animation without scrolling to top of table view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM