Cell Frame 個人化表格


一、新增UITableViewCell

uitableviewcell.swift
  • 宣告資料

    var label = UILabel()
    var cellleft = UIView()
    let WIDTH = UIScreen.main.bounds.size.width
    
  • 建立UILabel, UIImageView

    override init(style:UITableViewCellStyle, reuseIdentifier:String?) {
      super.init(style: style, reuseIdentifier: reuseIdentifier)
          label.frame = CGRect(x: 50, 
                               y: 0, 
                               width: 150, 
                               height: 50)
          label.textColor = .black
      contentView.addSubview(label)
          cellleft.frame = CGRect(x: WIDTH - 70, 
                               y: 0, 
                               width: 50, 
                               height: 50)
          cellleft.backgroundColor = .white
          cellleft.layer.cornerRadius = 25
      contentView.addSubview(cellleft)
    }
    required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }
    
  • 修改frame

    override var frame:CGRect {
      get {
          return super.frame
      }
      set(newFrame){
    
          let window = UIScreen.main.bounds
          let WIDTH = window.size.width
    
          var frame = newFrame
              frame.size.width = WIDTH - 45
              frame.size.height = 50
          super.frame = frame
      }
    }
    

二、ViewController.swift

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tv = UITableView()
    var numbers = Array(1...10)

    let WIDTH = UIScreen.main.bounds.size.width
    let HEIGHT = UIScreen.main.bounds.size.height

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .lightGray

            tv = UITableView(frame: CGRect(x: 0,
                                           y: 20,
                                           width: WIDTH,
                                           height: HEIGHT),
                             style: .plain)
            tv.delegate = self
            tv.dataSource = self
            tv.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
            tv.backgroundColor = .clear
            tv.allowsSelection = false
            tv.tableFooterView = UIView(frame: .zero)
        view.addSubview(tv)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 55
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return numbers.count
    }

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

        var cell: uitableviewcell! = tableView.dequeueReusableCell(withIdentifier: "cell") as? uitableviewcell // 轉型為自訂的uitableviewcell
        if cell == nil {
            cell = uitableviewcell(style: .default, reuseIdentifier: "cell")
        }

        cell.label.text = "測試資料   \(numbers[indexPath.row])"

        return cell
    }
}

下載完整範例 OneDrive

results matching ""

    No results matching ""