UITableView独立加载section



我目前有一个表视图需要时间在加载和有一个tableviewcell选择。我想在整个tableview加载之前在单独的section中显示这个预选单元格。我有要添加到单元格的数据。我能想到的一个丑陋的解决方案是有两个单独的表视图,但这不会是一个干净的解决方案。有什么建议吗,我怎样才能更好地处理这个场景?

在控制器中存储一个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

最新更新