我有一长串数据要显示在 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";
}
下面的图片会给你一个想法。给出了两个单元格,当点击心形图标时,它会重新加载以更新总爱的值。
最佳答案
NSIndexPath* ip = [NSIndexPath indexPathForRow:editedRowNumber inSection:0]; [self.tableViewTimeline scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:YES];
在类似的按钮操作中,您可以使用两种方式重新加载单元格而无需滚动,而最好只重新加载选定的索引作为建议@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/