改变uitableviewcell swift的宽度

时间:2015-03-24 09:01:16

标签: uitableview swift width cell

我有一个使用IB的tableView,其中包含自定义单元格和原型单元格。

我试图让细胞的宽度比tableView.frame略短,以在左右角之间留出一点空间。

var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
        cell.bounds = CGRectMake(10, self.tableView.frame.origin.y, 30, self.tableView.frame.size.height)
        cell.layer.bounds = CGRectMake(10, self.tableView.frame.origin.y, 30, self.tableView.frame.size.height)
        cell.textLabel?.bounds = CGRectMake(10, self.tableView.frame.origin.y, 30, self.tableView.frame.size.height)

更新:这是一个很好的示例,说明如何将subView添加到tableView。 http://natashatherobot.com/ios-frame-vs-bounds-resize-basic-uitableview-cell/

更新2:看起来没有简单的方法可以做到这一点。据我所知,有3种方法可以达到这个目的:

  1. 将圆形和较短的图像添加到具有相同颜色并与背景匹配的单元格中。
  2. 您可以继承tableViewCell,然后使用layoutSubviews,这样您可以在绘制单元格之前缩短它。我已经完成了,但滚动性能很糟糕。
  3. 最好的方法是完全抛弃tableView并使用collectionView重新执行它。

3 个答案:

答案 0 :(得分:8)

tableview中的单元格应该与容器一样宽。

如果您的单元格宽度与表格视图的宽度不同,我建议您将视图作为子视图添加到cell.contentView,并在确保contentView的同时将视图设置为您需要的宽度有明确的背景,没有分隔符和所有(所以它看起来不存在)。

另一个解决方案是通过向其添加左/右填充,使tableView不像superview宽。tableView。但是你会遇到这样的问题:在左侧和右侧,填充位置,您将无法滚动collectionView

我认为使用tableView是最干净的解决方案。它与{{1}}没有多大区别,您可以配置单元格的整个大小,而不仅仅是高度。

希望这可以帮助您解决问题。如果您需要更多帮助,请告诉我。

答案 1 :(得分:1)

斯威夫特3:

对于仍在寻找一个好解决方案的人来说,使用layoutSubviews或使用collectionView重新完成整个事情是一种更简单,更有效的替代方法。

如果您已经将tableViewCell子类化,那么在TableViewController类中,您可以添加它以在表格视图单元格的每一侧添加纯白色边框。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

          // make sure in storyboard that your cell has the identifier "cell" and that your cell is subclassed in "TableViewCell.swift"
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell

          // the following code increases cell border on all sides of the cell
          cell.layer.borderWidth = 15.0
          cell.layer.borderColor = UIColor.white.cgColor

          return cell;

}

如果要为单元格的每一侧添加不同大小的边框,也可以执行以下操作:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell


        // the following code increases cell border only on specified borders
        let bottom_border = CALayer()
        let bottom_padding = CGFloat(10.0)
        bottom_border.borderColor = UIColor.white.cgColor
        bottom_border.frame = CGRect(x: 0, y: cell.frame.size.height - bottom_padding, width:  cell.frame.size.width, height: cell.frame.size.height)
        bottom_border.borderWidth = bottom_padding

        let right_border = CALayer()
        let right_padding = CGFloat(15.0)
        right_border.borderColor = UIColor.white.cgColor
        right_border.frame = CGRect(x: cell.frame.size.width - right_padding, y: 0, width: right_padding, height: cell.frame.size.height)
        right_border.borderWidth = right_padding

        let left_border = CALayer()
        let left_padding = CGFloat(15.0)
        left_border.borderColor = UIColor.white.cgColor
        left_border.frame = CGRect(x: 0, y: 0, width: left_padding, height: cell.frame.size.height)
        left_border.borderWidth = left_padding

        let top_border = CALayer()
        let top_padding = CGFloat(10.0)
        top_border.borderColor = UIColor.white.cgColor
        top_border.frame = CGRect(x: 0, y: 0, width: cell.frame.size.width, height: top_padding)
        top_border.borderWidth = top_padding


        cell.layer.addSublayer(bottom_border)
        cell.layer.addSublayer(right_border)
        cell.layer.addSublayer(left_border)
        cell.layer.addSublayer(top_border)


        return cell;

}

希望这有帮助。

答案 2 :(得分:0)

我发现其他答案没有帮助/不正确,但在此处的其中一个答案中找到了我需要的内容:

How to set the width of a cell in a UITableView in grouped style

关键是,如果您有一个自定义单元格(OP 有,大多数应用程序都会有),那么您可以将 setFrame 方法覆盖为您需要的任何宽度。无需重新设计您的应用或做任何棘手的事情。

相关问题