【问题标题】:how to add xib file in table view cell?如何在表格视图单元格中添加 xib 文件?
【发布时间】:2025-12-28 01:40:12
【问题描述】:

我已经在那个 tableview 单元格中创建了可扩展的表格视图,我已经构建了 xib 文件,但我想知道如何在 xib 文件的循环中旋转?

自定义单元格1

@IBOutlet weak var middleLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!


ViewController
let items = ["Item 1", "Item2", "Item3", "Item4"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomCellOne")
    }

    // MARK: - UITableViewDataSource

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell

        cell.middleLabel.text = items[indexPath.row]
        cell.leftLabel.text = items[indexPath.row]
        cell.rightLabel.text = items[indexPath.row]

        return cell
    }

}

【问题讨论】:

    标签: ios iphone swift


    【解决方案1】:

    根据提供的信息,我猜你有 xib 和自定义 UITableViewCell 类。 这就是你需要的。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = Bundle.main.loadNibNamed("CellXib", owner: self, options: nil)?.first as! XibCustomCell
            return cell
    }
    

    【讨论】:

      【解决方案2】:

      请尝试以下代码

      class ViewController: UIViewController {
      
         @IBOutlet weak var tableView: UITableView!
         let items = ["Item 1", "Item2", "Item3", "Item4"]
      
         override func viewDidLoad() {
             super.viewDidLoad()
      
             self.tableView.register(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomOneCell")
             self.tableView.dataSource = self
             self.tableView.delegate   = self
        }
      
        //MARK:- UITableView DataSource & Delegate
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      
            return self.items.count
        }
      
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      
          let cell = tableView.dequeueReusableCell(withIdentifier: "CustomOneCell") as! CustomOneCell
      
          cell.middleLabel.text = items[indexPath.row]
          cell.leftLabel.text = items[indexPath.row]
          cell.rightLabel.text = items[indexPath.row]
      
          return cell
        }
      
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          return 44.0 //give height you want
        }
      }
      

      【讨论】:

      • 这不会将自定义单元格从 xib 文件中出列。如果您在故事板中设计了一个自定义的UITableViewCell,则可以使用它以防万一。使用自己的UITableView
      • 我为 tableViewCell 创建了一个 xib。为了在 tableview 中使用,我在 tableview 注册。
      【解决方案3】:

      【讨论】:

      • 不是发布答案的可靠方式。
      最近更新 更多