【问题标题】:How do i refresh the particular Section?如何刷新特定部分?
【发布时间】:2016-11-08 11:53:15
【问题描述】:

我的ViewController 上有一个UITableView 它有n 的部分,我的第一部分总是显示我的愿望清单 产品(仅基于显示愿望清单SettingsController 中启用了选项) 每次我在DetailViewController 上将产品添加到我的愿望清单 时,我都会触发NSNotificatiion 到我的ViewController它将在哪里获取 Wishlist 记录。

我的numberOfRowsInSection: 总是返回1,因为它是一个UITableView + UICollectionView 组合来产生水平+垂直滚动。

所以,我正在重新加载我的部分,如下所示,

if (isWishListSectionReloadRequires) {
    NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
                                 [NSIndexPath indexPathForRow:0 inSection:0],
                                 nil];
    NSArray *insertIndexPaths = [NSArray arrayWithObjects:
                                 [NSIndexPath indexPathForRow:0 inSection:0],
                                 nil];
    [tableView beginUpdates];
    [tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationNone];
    [tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
} else {
    [tableView reloadData];
}

但是,我正面临崩溃,

-[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1054 2016-11-04 04:25:15.921 估计 [9025:c07] 中的断言失败 *** 由于应用程序终止未捕获的异常“NSInternalInconsistencyException”,原因:“无效更新:无效的节数。更新后表视图中包含的节数(10)必须等于更新前表视图中包含的节数(10),加上或减去插入或删除的节数(1插入,1已删除)

谁能告诉我可能是什么原因?任何想法将不胜感激!

【问题讨论】:

  • 您是否相应地更新了数据源?我的意思是一个部分的行数等?
  • @KrishnaChaitanyaAmjuri 如何手动更新数据源?
  • 试试 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation,而不是删除和插入。
  • 您是否尝试通过调用tableview 的reloadSections 方法重新加载第一部分?
  • 是的,我试过了。它崩溃了

标签: ios objective-c uitableview uiviewcontroller


【解决方案1】:

您可以改用 UITableView 的 reloadSections 方法。

目标 C

NSRange range = NSMakeRange(0, 1);
NSIndexSet *section = [NSIndexSet indexSetWithIndexesInRange:range];                                     
[self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationNone];

斯威夫特

tableView.reloadSections(NSIndexSet(index: 2), withRowAnimation: .None)

【讨论】:

    【解决方案2】:

    您可以重新加载收藏视图,因为您只需要更新心愿单。

    [collectionView reloadData];
    

    如果你需要水平和垂直滚动,有很多第三方库一旦结帐RTTwoDimmensionalScroll

    【讨论】:

      【解决方案3】:

      使用以下方式重新加载,不要忘记在开始更新和结束更新中重新加载特定部分,以便页眉和页脚也得到更新。

      [tableView beginUpdates];
      NSIndexSet *section = [NSIndexSet indexSetWithIndex:0];                                     
      [self.tableView reloadSections:section withRowAnimation:UITableViewRowAnimationAutomatic];
      [tableView endUpdates];
      

      【讨论】:

        猜你喜欢
        • 2011-08-08
        • 1970-01-01
        • 1970-01-01
        • 2018-06-12
        • 2011-12-23
        • 2016-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多