代码之家  ›  专栏  ›  技术社区  ›  typeoneerror

在自定义UITableView中显示空白的UITableView单元格

  •  12
  • typeoneerror  · 技术社区  · 15 年前

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

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

    使用默认的TableView样式,我可以获取重复的空行来填充视图(例如,邮件应用程序具有此样式)。我尝试将uiTableView上的背景色图案设置为相同的平铺背景:

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

    …但是图块比TableView的顶部稍早一点,因此一旦实际单元格完成显示,图块就关闭:

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

    如何自定义TabLVIEW,但如果行数少于填充页,仍然保留空白行?

    2 回复  |  直到 15 年前
        1
  •  9
  •   TechZen    15 年前

    你偶然删除了背景色和分隔符样式吗?如果你这样做了,这就是为什么没有额外的细胞。我认为默认的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;
    }
    
        2
  •  0
  •   TechZen    15 年前

    我认为最上面一行的轻微错位是由TableView的垂直滚动反弹引起的。如果关闭该选项,顶行应正确对齐。

    另外,您可以返回一个单元格高度,该高度将包含平铺 tableview:cellHeightForRow: . 这对默认单元格很有效。