Wiz*_*iOS 177 objective-c uitableview
我有自定义UITableView使用UITableViewCells.每个UITableViewCell都有2个按钮.单击这些按钮将更改UIImageView单元格内的图像.
是否可以单独刷新每个单元格以显示新图像?任何帮助表示赞赏.
Rom*_*ain 319
获得单元格的indexPath后,您可以执行以下操作:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathOfYourCell, nil] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
在Xcode 4.6及更高版本中:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:@[indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
Run Code Online (Sandbox Code Playgroud)
当然,你可以设置任何你喜欢的动画效果.
ma1*_*w28 34
我试着打电话-[UITableView cellForRowAtIndexPath:],但那没用.但是,以下内容适用于我.我alloc和release该NSArray用于紧密内存管理.
- (void)reloadRow0Section0 {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[indexPaths release];
}
Run Code Online (Sandbox Code Playgroud)
Esq*_*uth 22
迅速:
func updateCell(path:Int){
let indexPath = NSIndexPath(forRow: path, inSection: 1)
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) //try other animations
tableView.endUpdates()
}
Run Code Online (Sandbox Code Playgroud)
Mac*_*nik 18
reloadRowsAtIndexPaths:很好,但仍然会迫使UITableViewDelegate方法开火.
我能想到的最简单的方法是:
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
[self configureCell:cell forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)
这是重要的调用你configureCell:的主线程中执行,因为它不会在非UI线程工作(同样的故事reloadData/ reloadRowsAtIndexPaths:).有时添加可能会有所帮助:
dispatch_async(dispatch_get_main_queue(), ^
{
[self configureCell:cell forIndexPath:indexPath];
});
Run Code Online (Sandbox Code Playgroud)
同样值得避免在当前可见视图之外完成的工作:
BOOL cellIsVisible = [[self.tableView indexPathsForVisibleRows] indexOfObject:indexPath] != NSNotFound;
if (cellIsVisible)
{
....
}
Run Code Online (Sandbox Code Playgroud)
App*_*Guy 16
如果您使用自定义TableViewCells,则通用
[self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)
除非您离开当前视图并返回,否则无法有效回答此问题.第一个答案也没有.
要成功重新加载第一个表视图单元而不切换视图,请使用以下代码:
//For iOS 5 and later
- (void)reloadTopCell {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}
Run Code Online (Sandbox Code Playgroud)
插入以下刷新方法,该方法调用上述方法,以便您可以自定义仅重新加载顶部单元格(如果您愿意,还可以重新加载整个表格视图):
- (void)refresh:(UIRefreshControl *)refreshControl {
//call to the method which will perform the function
[self reloadTopCell];
//finish refreshing
[refreshControl endRefreshing];
}
Run Code Online (Sandbox Code Playgroud)
现在你已经对它进行了排序,在你的内部viewDidLoad添加以下内容:
//refresh table view
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];
Run Code Online (Sandbox Code Playgroud)
您现在有一个自定义刷新表功能,将重新加载顶部单元格.要重新加载整个表,请添加
[self.tableView reloadData]; 到你的新刷新方法.
如果您希望每次切换视图时重新加载数据,请实现以下方法:
//ensure that it reloads the table view data when switching to this view
- (void) viewWillAppear:(BOOL)animated {
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
tableView.beginUpdates()
tableView.reloadRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
149491 次 |
| 最近记录: |