【问题标题】:push tableview cell frame in cgrect to margin将 cgrect 中的 tableview 单元格框架推到边距
【发布时间】:2020-08-09 20:56:38
【问题描述】:

在下面的快速代码中,我希望将 tableview 单元格推到边缘,就像左侧的单元格一样。在下面的照片中,您可以看到右侧的 tableview 单元格的边距比左侧的要大得多。请让他们平等。

pic of tableview

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(indexPath.row)"
        return cell
    }
    var numberOfRows = 3
  
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { numberOfRows }

 
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 118
    }
    var btn = UIButton()
    var tableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        setTableVIew()
        
      
    }
    
    func setTableVIew(){

        
        let VCframe = self.view.frame
        let height = VCframe.height * 0.8
        let widthx = VCframe.width
        
        
        tableView.frame = CGRect(x: 0, y: 0, width: widthx, height: height)

        tableView.delegate = self
        tableView.dataSource = self
        self.view.addSubview(tableView)
        tableView.backgroundColor = .blue
        
        tableView.register(customtv.self, forCellReuseIdentifier: "cell")
    }


}
class customtv: UITableViewCell {
    
   lazy var backView : UIView = {
    let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width  , height: 110))
    view.backgroundColor = .green
    print(self.frame.width)
        return view
    }()
    

    
    override func layoutSubviews() {
       backView.clipsToBounds = true

    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(animated, animated: true)
        addSubview(backView)
    }
}

【问题讨论】:

  • 图像中没有左或右...

标签: swift uitableview tableview frame cgrect


【解决方案1】:

在 layoutSubviews 中更新 backView 框架

class customtv: UITableViewCell {
    
   lazy var backView : UIView = {
    let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width  , height: 110))
    view.backgroundColor = .green
    print(self.frame.width)
        return view
    }()
    

    
    override func layoutSubviews() {
       backView.clipsToBounds = true
       backView.frame =  CGRect(x: 0, y: 6, width: bounds.maxX  , height: 110)

    }

它们将成为等宽的单元格...如果您想提供边距...将其提供给您的表格视图而不是单元格...

tableView.frame = CGRect(x: 10, y: 0, width: widthx - 20, height: height)

【讨论】:

  • 我在 backView = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110) 处收到错误说明无法将“CGRect”类型的值分配给“UIView”类型
猜你喜欢
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 2012-11-24
  • 2020-03-04
  • 2012-08-28
  • 1970-01-01
相关资源
最近更新 更多