【问题标题】:Show blank UITableViewCells in custom UITableView在自定义 UITableView 中显示空白 UITableViewCells
【发布时间】:2011-02-06 12:57:22
【问题描述】:

我正在尝试自定义 UITableView。到目前为止,它看起来不错。但是当我使用自定义 UITableViewCell 子类时,当只有 3 个单元格时,我没有得到空白表格单元格:

alt text http://img193.imageshack.us/img193/2450/picture1zh.png

使用默认的 TableView 样式,我可以获得重复的空白行来填充视图(例如,邮件应用程序有这个)。我尝试将 UITableView 上的 backgroundColor 模式设置为相同的磁贴背景:

UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"score-cell-bg.png"]];
moneyTableView.backgroundColor = color;

...但是磁贴在 TableView 顶部之前开始一点,所以一旦实际单元格完成显示,磁贴就会关闭:

alt text http://img707.imageshack.us/img707/8445/picture2jyo.png

如果行数少于填满一页,如何自定义我的 tableview 但仍保留空白行?

【问题讨论】:

  • +1 这是一个很好的问题。我希望每个人,包括我自己,都花这么多时间来写一个好问题。
  • 我试图提出同样的问题,但发现了两种不同的方法。作为来自谷歌的人的参考,请查看使用自定义页脚视图的this other answer

标签: iphone uitableview


【解决方案1】:

我认为顶行的轻微错位是由 tableview 的垂直滚动反弹引起的。如果你关闭它,顶行应该正确对齐。

此外,您可以只返回包含tableview:cellHeightForRow: 中磁贴的单元格高度。这很好地适用于默认单元格。

【讨论】:

    【解决方案2】:

    您是否偶然删除了背景颜色和分隔符样式?如果你这样做了,那可能就是没有额外单元格的原因。我认为默认的 UITableView 并没有真正添加更多的单元格,它只是具有分隔符样式来创建这种错觉,并且因为它具有白色背景,所以它们看起来像单元格。

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    

    如果不是这样,您可以随时尝试添加无法选择的额外单元格:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return ([source count] <= 7) ? 7 : [source count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
        // Set all labels to be blank
        if([source count] <= 7 && indexPath.row > [source count]) {
            cell.textLabel.text = @"";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        } else {
            cell.textLabel.text = [source objectAtIndex:indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        }
    
      return cell;
    }
    

    【讨论】:

    • “它只是具有分隔符样式来创造这种错觉”你完全正确。所以,我想我只是要重新添加分隔符并使背景图像具有两列颜色,而不是让分隔符 in 作为背景图像...
    • 很好,效果很好!仍然需要做一些调整。 img517.imageshack.us/img517/5091/picture3lt.png
    • 不应该 if([source count] &lt;= 7 &amp;&amp; indexPath.row &gt; [source count]) {if([source count] &lt;= 7 &amp;&amp; indexPath.row &gt; ([source count] - 1)) { 因为索引路径从 0 开始,而计数从 1 开始
    • 这不是一个真正面向未来的方法。尤其是现在我们需要开发至少 4 种不同的屏幕高度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    相关资源
    最近更新 更多