如何刷新tableViewCell上添加的一个View

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何刷新tableViewCell上添加的一个View相关的知识,希望对你有一定的参考价值。

//一个section刷新
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

//一个cell刷新
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
参考技术A 1、出现刷新闪动问题的是CollectionViewCell,我当时以为tableView也会有同样的问题,所以问题里写成了tableViewCell。实时证明,我当时的场景tableView不会出现闪动问题。

解决方案:
1、针对原问题CollectionViewCell闪动,是由于CollectionView隐式动画引起的,可以这样写:

//关闭隐式动画,防止cell闪动
[UIView performWithoutAnimation:^
[self reloadItemsAtIndexPaths:@[indexP]];
];
更多关掉隐式动画方案:http://adad184.com/2015/11/10...
2、针对tableViewCell的刷新,可以在自定义cell写一个方法实现,但是滑动到可见范围之外再回来,状态会回复(因为走了tableView:tableViewcellForRowAtIndexPath)。如果要保持cell的状态不改变只能通过reload方式实现,就是更新数据源。

删除firebase中的数据后如何刷新TableViewCell?

【中文标题】删除firebase中的数据后如何刷新TableViewCell?【英文标题】:How to refresh TableViewCell after deleting data in firebase? 【发布时间】:2018-12-20 16:07:24 【问题描述】:

我想在通过滑动操作表删除对话后刷新我的对话(表格视图单元格)。

我尝试在删除数据后重新加载表格视图,但它不起作用。还有异步。

 // Swipe Action Sheet solution
func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?


    // Write action code for the Flag
    let deleteAction = UIContextualAction(style: .normal, title:  "Löschen", handler:  (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        let ref = Database.database().reference()
        ref.child("users").child((self.currentUser?.uid)!).child("conversations").child((self.items[indexPath.row].user.uid)!).removeValue()
        DispatchQueue.main.async 
            self.tableView.reloadData()
        
        success(true)
    )
    deleteAction.backgroundColor = .red
    return UISwipeActionsConfiguration(actions: [deleteAction])


提前感谢您的帮助!

【问题讨论】:

【参考方案1】:

如果没有任何错误,您必须在removeValue 方法的完成块中重新加载数据。此外,在重新加载数据之前,您必须从 items 数组中删除项目

.removeValue  error,_ in
    if error == nil 
        self.items.remove(at: indexPath.row)
        self.tableView.reloadData() // self.tableView.deleteRows(at: [indexPath], with: .automatic)
    
    success(error == nil)

【讨论】:

这个完成块是否在主队列中被调用?如果没有,请确保将 UI 更新强制放在主队列中。 @rmaddy ***.com/a/39183023/10253094 但我不确定,是这种情况吗? @rmaddy 我在这里也没有找到任何相关信息:firebase.google.com/docs/reference/ios/firebasedatabase/api/…:【参考方案2】:

要使用滑动删除行,您可以使用

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) 
        if editingStyle == .delete 
            items.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .fade)
         
    

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool 

    return true

而且因为您没有重新加载表格视图,所以看起来会更好

【讨论】:

以上是关于如何刷新tableViewCell上添加的一个View的主要内容,如果未能解决你的问题,请参考以下文章

iOS:如何在其附件视图更改时触发 tableViewCell 的约束/自动布局刷新?

如何将子视图添加到 TableViewCell?

LINUX如何改屏幕刷新频率

在重新排序时在 tableviewcell 上添加阴影 [关闭]

ios tableviewcell 怎么刷新界面

如何在swift中将标题视图添加到每个自定义tableViewCells