我是 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/