iOS - 如何仅在 Table 为空时增加 UITableView 第一行的高度?

标签 ios uitableview

我是 iOS 开发新手。

我想检查我的 table 是否为空。如果是,我想:


增加第一行的高度并显示“没有新消息!”

去掉表格,只在页面中央显示“No new messages”


我该怎么做?

最佳答案

(假设您有一个要从中填充 TableView 的数组。这是填充 TableView 的一种非常标准的方法。我们称这个理论数组为 dataArr。)

在您的数据源中:

- (NSUInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSUInteger)section
{
    if ([dataArr count] == 0) {
        // empty table
        return 1;
    }
    return [dataArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)ip
{
    UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"someCellID"];
    if (cell == nil)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"someCellID"] autorelease];

    if ([dataArr count] == 0) {
        // empty table
        cell.textLabel.text = @"No new messages";
    } else {
        // configure as normally
    }
    return cell;
}

在你的委托(delegate)中:

- (CGFloat)tableView:(UITableView *)tv heightForRowAtIndexPath:(NSIndexPath *)ip
{
    if ([dataArr count] == 0) {
        // empty table
        return 88.0f; // 2 times the normal height
    }
    // else return the normal height:
    return 44.0f;
}

关于iOS - 如何仅在 Table 为空时增加 UITableView 第一行的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283805/

相关文章:

ios - TableView 的自定义原型(prototype)单元格

arrays - 从最低到最高对表格 View 单元格进行排序

ios - UITableView 和 UIView 手势冲突

ios - 加载 XIB 此类与键的键值编码不兼容

ios - 以编程方式添加 UIPickerView

objective-c - 如何正确链接自定义 UITableViewCell 和访问元素

ios - 在 Swift 中加载其他元素后,在 TableViewCell 中异步加载一个元素

UINavigationController 中的 UITableView 旋转后进入导航栏

ios - 带参数的顺序调用方法 cocos2d-x v3.1

ios - 我是否必须在 ARC 下调用 dispatch_release?