UITableView无数据屏幕

Yaz*_*zmi 8 iphone objective-c uitableview

我的UITableView从互联网上获取数据.有时它不会收到任何(细胞)数据.它只显示一张空白表.

如何向用户显示未找到数据记录的消息/单元格?

Dan*_*Ray 25

您希望返回一个报告缺少数据的单元格.

如果你将你的单元格数据保存在一个类属性的数组中(比方说NSArray *listings),你可以去:

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    if ([self.listings count] == 0) {
         return 1; // a single cell to report no data
    }
    return [self.listings count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.listings count] == 0) {
        UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease];
        cell.textLabel.text = @"No records to display";
        //whatever else to configure your one cell you're going to return
        return cell;
    }

    // go on about your business, you have listings to display
}
Run Code Online (Sandbox Code Playgroud)

  • 但是,如果我们使用insertRowsAtIndexPaths或deleteRowsAtIndexPaths rows方法,这种方法将破坏应用程序.添加第一个删除最后一个单元格将导致错误,因为方法numberOfRowsInSection返回1,但数据源数组为空. (2认同)