美文网首页
swift-tableView

swift-tableView

作者: 雷霆嘎巴嘎嘎 | 来源:发表于2022-05-17 08:03 被阅读0次

swift中tableViewcell的配置有两种方式

  • 需要注册cell,注册cell带forIndexPath
    var tableView : UITableView!
    static let cellId = "cellIdl"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        
        tableView = UITableView.init(frame: self.view.bounds, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        //cell注册
        tableView.register(marketCell.self, forCellReuseIdentifier: MarketViewController.cellId)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {        
        let cell = tableView.dequeueReusableCell(withIdentifier: MarketViewController.cellId, for: indexPath) as! marketCell
        
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }

  • 不用注册cell
    var tableView : UITableView!    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        tableView = UITableView.init(frame: view.bounds, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          var cell = tableView.dequeueReusableCell(withIdentifier: "cellIdl")
          if cell == nil {
              cell = UITableViewCell.init(style: .default, reuseIdentifier: "cellIdl")
           }
          return cell!        
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 50
    }

注册的方法会自动帮我们完成分配新单元格这个工作 而非注册则需要我们自己手动创建新的单元格
1.如果注册,当标识符为identifier 的Cell队列中没有可复用的cell时,系统会自动创建该类型的cell。
2.如果没有注册,需要判定是否有无可复用的cell,并手动新建一个cell。
注册后就不用管cell是否需要新建了

tip: OC中没有注意这个点。。。。好像oc中也有这个规则,奇奇怪怪

相关文章

  • swift-tableView

    swift中tableViewcell的配置有两种方式 需要注册cell,注册cell带forIndexPath ...

  • swift-TableView初体验

    尝试下swift些UI的快感,然而第一接触还是有点不顺手,一个TableView也研究了一下,方便了许多。都说sw...

  • Swift-TableView使用及编程技巧

    tableView的headView的隐藏 注意: 无论是设置hidden,还是修改frame,还是removeF...

网友评论

      本文标题:swift-tableView

      本文链接:https://www.haomeiwen.com/subject/melwurtx.html