ios - UITableView cellForRowAtIndexPath : How to return no cell

标签 ios uitableview

当没有数据从 Parse 返回时,我试图从 TableView 中删除表中的一个单元格。但无论我尝试什么,我都找不到任何解决方案来避免在包含内容的单元格之前出现空白单元格(未传递数据时的单元格)。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object

static NSString *simpleTableIdentifier = @"RecipeCell";

    NSString *identifier;
    if (indexPath.row == 0) {
        identifier = @"Cell";
    } else if (indexPath.row == 1) {
        identifier = @"Cell2";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];


    }

// Configure the cell

if (![@"test"  isEqual: [object objectForKey:@"teacher"]]){

    UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
    nameLabel.text = @"";

    UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
    prepTimeLabel.text = @"";


return cell;

} else {

    UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
    nameLabel.text = [object objectForKey:@"message"];

    UILabel *prepTimeLabel = (UILabel*) [cell viewWithTag:102];
    prepTimeLabel.text = [object objectForKey:@"teacher"];

    return cell;

}
}

感谢您提供的任何帮助!

最佳答案

您无法通过 cellForRowAtIndexPath 方法控制 UITableView 中的单元格数量 - 您需要从 numberOfRowsInSection 方法返回正确的值.引用UITableViewDataSource protocol referenceTable View Programming Guide

此外,此代码块 -

if (indexPath.row == 0) {
    identifier = @"Cell";
} else if (indexPath.row == 1) {
    identifier = @"Cell2";
}

有点多余 - 您的所有单元格都是 UITableViewCellStyleDefault,因此它们可以互换。没有必要使用两个不同的标识符 - 如果您打算在将来使用不同的自定义单元格类型,那么您可以保留这个概念,但您可能不会直接使用 indexPath.row,而是由数据类型驱动相关行的模型。

关于ios - UITableView cellForRowAtIndexPath : How to return no cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22544447/

相关文章:

ios - 父子 UIViewControllers IOS

swift - 向 UINavigationBar 添加 subview 时将 UITableView 向下推

ios - 如何将纬度值传递给 iPhone 中的下一个 View ?

iOS 9 Alamofire 从不加载图像

ios - 将核心数据上保存的数据填充到 tableView 上

ios - 远距离颜色检测(Swift)

ios - 账户工具包 : Set the default language for iOS app

ios - 具有错误的 IndexPath 影响的 UITableViewCells 和动画坐标问题

ios - 核心数据 ios9 : multifield unique constraint

ios - 使用 AFNetworking 进行基本身份验证