【问题标题】:UITableView's background color becoming white when section is empty当部分为空时,UITableView 背景颜色变为白色
【发布时间】:2009-12-14 05:27:20
【问题描述】:

这是交易:我有一个包含 2 个部分的 UITableView,我想在第一部分为空时显示一个“无数据”单元格,这样 2 个部分的标题就不会粘在一起(因为它看起来很奇怪) .

它工作得很好(尽管我一开始很难让它工作,see this thread)。我正在使用 viewForFooterInSection :

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

if(section == 0)
{
        if([firstSectionArray count] == 0)
                return 40;
        else 
                return 0;
}

return 0;

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if(section == 0)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 44)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
        label.textAlignment = UITextAlignmentCenter;
        label.lineBreakMode = UILineBreakModeWordWrap; 
        label.numberOfLines = 0;
        label.text = @"No row";
        return [label autorelease];
    }

    return nil;
}

但是当我显示部分页脚视图时,背景颜色变成纯白色。见图片:

alt text http://img683.yfrog.com/img683/9480/uitableviewproblem.png

当背景充满空单元格时,我更喜欢它。有谁知道该怎么做?谢谢

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    当您没有页脚时,背景会被空单元格填充。所以不要实现viewForFooterInSection(或titleForFooterInSection)方法,会得到“空单元格”效果。

    我建议您返回一个单元格,指示没有要显示的条目,如下所示:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (matches.count>0) {
            // Do your usual thing here
        } else {
            static NSString *cellId = @"noDataCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId] autorelease];
                cell.textLabel.textAlignment = UITextAlignmentCenter;
                cell.textLabel.textColor = [UIColor grayColor];
            }
            cell.textLabel.text = @"Aucun match";
            return cell;
        }
    }
    

    当然,您必须告诉 UIKit,您的部分中始终至少有一个单元格...我添加了 isDeletingRow 似乎给您带来麻烦的案例(在评论中)。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (section==0) return matches.count>0 ? matches.count : (isDeletingRow ? 0 : 1);
        // Set 'isDeletingRow' to YES when a delete is being committed to the table, in that case we let UIKit know that we indeed took care of the delete...
        // And cover the other sections too...
    }
    

    提交编辑时,您需要将isDeletingRow 设置为numberOfRowsInSection 以返回令人满意的值...

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            isDeletingRow = YES;
            // Your current code when a row is deleted here...
            isDeletingRow = NO;
            if (matches.count==0) [self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.5];
        }
    }
    

    【讨论】:

    • 我已经尝试过了,但是当我删除该部分的最后一行时出现 NSInternalInconsistencyException:'无效更新:第 0 部分中的行数无效。之后现有部分中包含的行数更新 (1) 必须等于更新 (1) 之前该节中包含的行数,加上或减去从该节插入或删除的行数(0 插入,1 删除)。这让我发疯了!
    • 我已经更新了答案,向您展示了如何解决该问题 :) 您确实需要将该部分的行数设为 0,但也无法完成这项工作...
    • 非常感谢卓然!有用! (虽然还是有点老套,但我想我们这里别无选择……)
    • 我没有找到更好的方法 :) UIKit 检查是否确实删除了一行是正确的......但是在这里你真的希望最后一行被“Aucun match”行替换,而使用“删除”功能......它必须有点hacky,因为我们正在劫持“删除”以用作“替换”...... :)
    • 我猜你是对的 :) 但我更喜欢 viewForFooterInSection 解决方案,可惜我们无法避免恼人的白色背景!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2023-03-10
    • 2019-07-01
    • 2015-02-17
    • 2016-08-29
    相关资源
    最近更新 更多