ios - 重新加载 UITableView 数据并向下滚动到最后选择的单元格

标签 ios objective-c uitableview

我有一长串数据要显示在 UITableView 中.表格的一个单元格包含许多内容,例如评论、分享和点赞。现在,当用户喜欢单元格中的帖子时,reloadTableData并再次移动到第一个。例如,用户向下滚动到 30 个单元格,然后像第 29 个单元格一样,它将重新加载数据并再次回到顶部。那么如何在重新加载数据后使其保持在同一个地方?这是我的代码:

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

而对于 Like 按钮的实现
- (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按钮单击。
-(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 of app

最佳答案

  • 在变量
  • 中保存已编辑的索引
  • 获取该索引的索引路径
  • 在 reoladdata 到该索引后滚动到表

  • NSIndexPath* ip = [NSIndexPath indexPathForRow:editedRowNumber inSection:0];
        [self.tableViewTimeline scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];
    

  • 如果您的表格 View 有一个部分,您可以传递零,如果有部分您必须传递用户编辑单元格的部分

  • 在类似的按钮操作中,您可以使用两种方式重新加载单元格而无需滚动,而最好只重新加载选定的索引作为建议@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;
    }
    

    关于ios - 重新加载 UITableView 数据并向下滚动到最后选择的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31918130/

    相关文章:

    iphone - 如何检测 UITableView 上的 "quick touch"以便切换到全屏或从全屏切换?

    ios - MvvmCross - Root View Controller

    objective-c - 你如何使用移动平均线来过滤掉 iPhone OS 中的加速度计值

    php - iOS 应用程序从服务器获取数据

    iphone - 设置UIActivityIndi​​catorView的框架

    iphone - ARC 是否意味着我应该声明静态类型而不是 id?

    iOS 6+ : UITableViewController embedded into Navigation Controller shows table's first section header under status bar when Navigation Bar is hidden

    Swift UITableViewCell subview 不注册点击

    ios - TWTRTweetTableViewCell 无法检测链接 - Fabric TwitterKit

    iphone - 应用程序因在第三代 iPad 上崩溃而被拒绝