【问题标题】:change width of tableviewcell swift快速改变tableviewcell的宽度
【发布时间】:2015-05-27 12:28:57
【问题描述】:

我有一个使用带有自定义单元格和原型单元格的 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 重新做。

【问题讨论】:

    标签: uitableview swift width cell


    【解决方案1】:

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

    如果您需要您的单元格具有与表格视图不同的宽度,我建议将视图作为子视图添加到 cell.contentView 并根据需要使该视图尽可能宽,同时确保 contentView 具有清晰的背景和没有分隔符和所有(因此看起来它不存在)。

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

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

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

    【讨论】:

    • Multumesc Catalina,看起来你是对的。我将不得不添加一个子视图。我用我找到的一个很好的例子更新了我的问题。
    【解决方案2】:

    斯威夫特 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;
    
    }
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢,但这不是改变单元格宽度!
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多