【问题标题】:UITableView load sections independentlyUITableView 独立加载部分
【发布时间】:2016-08-23 17:25:57
【问题描述】:

我目前有一个表格视图,它需要一些时间来加载并选择了一个表格视图单元。我想在加载整个表格视图之前在单独的部分中显示该预先选择的单元格。我有要添加到单元格的数据。我能想到的一个丑陋的解决方案是有两个单独的表格视图,但这不是一个干净的解决方案。有什么建议,我该如何更好地处理这种情况?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    在控制器中存储一个 BOOL 值,可能命名为“dataLoaded”。然后,更新您的表视图数据源方法:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        if (self.dataLoaded) {
            return 1; //Just the pre-selected cell
        }
        return 2; //However many sections for when data loaded
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (section==0) {
            return 1; //Your pre-selected cell
        else
            return self.numberOfData; //Number of cells in other section after data loaded
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section==0) {
           //Return your pre-selected cell
        } else {
           //Data cells (after loaded)
        }
    }
    

    现在,在 viewDidLoad 中,只需调用 [tableView reloadData]。然后,一旦数据加载完毕,再次调用 reloadData

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      相关资源
      最近更新 更多