【问题标题】:spacing between UITableViewCells swift3UITableViewCell swift 3之间的间距
【发布时间】:2019-01-01 00:11:26
【问题描述】:

我正在快速创建一个 IOS 应用程序,并希望像这样在单元格之间添加间距

我想为每个表格视图单元格提供与我的附加图像相同的空间。 我怎么能这样做?现在所有的细胞都没有任何空间。 迅速3

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我不知道我该如何解决这就是为什么要在这里发帖 - dahiya_boy
  • 检查我的答案。要获得此演示,请给我您的电子邮件 ID。

标签: ios uitableview swift3 cellspacing


【解决方案1】:

你可以在你的 tableView 单元格中试试这个:

class cell: UITableViewCell{
override var frame: CGRect {
    get {
        return super.frame
    }
    set (newFrame) {
        var frame =  newFrame
        frame.origin.y += 4
        frame.size.height -= 2 * 5
        super.frame = frame
    }
  }
}

【讨论】:

  • @Ankit Kumawat 让我知道是否发现此答案错误/正确!
【解决方案2】:

更新:UIEdgeInsetsInsetRect 方法已被替换,现在你可以这样做了

contentView.frame = contentView.frame.inset(by: margins)

Swift 4 答案:

在您的自定义单元格类中添加此功能

override func layoutSubviews() {
    super.layoutSubviews()
    //set the values for top,left,bottom,right margins
            let margins = UIEdgeInsets(top: 0, left: 0, bottom: 10, right: 0)
    contentView.frame = UIEdgeInsetsInsetRect(contentView.frame, margins)
}

您可以根据需要更改值

***** 注意 ***** 调用超级函数

super.layoutSubviews()

非常重要,否则你会遇到奇怪的问题

【讨论】:

    【解决方案3】:
    1. 在 Storyboard 中,您的视图层次结构应该是这样的。 View CellContent(突出显示)将包含所有组件。

    1. View CellContent 提供距其父视图顶部、底部、前导和尾随 10 像素的边距。

    2. 现在,选择tblCell 并更改背景颜色。

    1. 现在运行您的项目,确保委托和数据源已正确绑定。

    输出

    注意:我刚刚在 View CellContent 中添加了 1 个 UILabel 用于虚拟目的。

    【讨论】:

      【解决方案4】:

      如果您使用UITableViewCell 来实现这种布局,则没有规定在UITableViewCells 之间提供间距。

      您可以选择以下选项:

      1. 使用clear backgroundUITableViewCell 内创建自定义UIView,使其看起来像单元格之间的间距。

      你需要设置background as clearcell, content view

      1. 您可以使用UICollectionView 代替UITableView。它更加灵活,您可以按照自己的方式进行设计。

      如果您想了解更多详情,请告诉我。

      【讨论】:

      • 我已经在 UITableViewCell 中设计了自定义 UIView,背景清晰,但单元格之间没有出现间距
      • 你能显示视图层次结构吗?以及你得到的输出
      • 更新了答案。
      【解决方案5】:

      一种简单的方法是集合视图而不是表格视图,并为集合视图提供单元格间距并使用

      func collectionView(_ collectionView: UICollectionView,
                          layout collectionViewLayout: UICollectionViewLayout,
                          sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
          let widthSize = collectionView.frame.size.width / 1
          return CGSize(width: widthSize-2, height: widthSize+20)
      }
      

      如果你只想要表格视图,那么添加背景视图作为容器视图并将背景颜色设置为白色和单元格背景颜色清除颜色设置单元格前导、颤音、底部的背景视图为 10

      backgroundView.layer.cornerRadius = 2.0
      backgroundView.layer.masksToBounds = false
      backgroundView.layer.shadowColor = UIColor.black.withAlphaComponent(0.2).cgColor
      

      【讨论】:

      • 很抱歉,但您的回答不正确无济于事 - 234 Shk
      【解决方案6】:

      请试一试。它对我来说很好用。

      您可以使用部分而不是行。 您在numberOfSectionsInTableView 方法中返回数组计数并在numberOfRowsInSection 委托方法中设置1

      cellForRowAtIndexPath 方法中使用[array objectAtIndex:indexPath.section]

      heightForHeaderInSection 设置为 40 或根据您的要求。

      谢谢,希望对你有帮助

      【讨论】:

      • 如果要处理多个部分,你会怎么做
      【解决方案7】:

      - 静态设置 UITableViewCell 间距 - Swift 4 - 未完全测试。

      将您的 tableView 行高设置为您喜欢的任何值。

       override func viewDidLoad() {
          super.viewDidLoad()
          tableView.estimatedRowHeight = <Your preferred cell size>
          tableView.rowHeight = UITableViewAutomaticDimension
          // make sure to set your TableView delegates
          tableView.dataSource = self
          tableView.delegate = self
      }
      
       extension YourClass : UITexFieldDelegate, UITextFieldDataSource {
          //Now set your cells.
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "yourCell", for: indexPath) as! UITableViewCell
      
              //to help see the spacing.
              cell.backgroundColor = .red
              cell.textLabel?.text = "Cell"
      
              return cell
         }
         //display 3 cells
         func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return 3
         }
      
         //now lets insert a headerView to create the spacing we want. (This will also work for viewForHeaderInSection)
         func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
      
          //you can create your own custom view here
          let view = UIView()
          view.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 44) //size of a standard tableViewCell
      
          //this will hide the headerView and match the tableView's background color.
          view.backgroundColor = UIColor.clear
          return view
       }
      
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
              return 44
        }
      }
      

      【讨论】: