【问题标题】:How to detected nib file table view cell to make ReusableCell?如何检测 nib 文件表格视图单元格以制作 ReusableCell?
【发布时间】:2016-11-02 19:51:37
【问题描述】:

我需要添加单元格标识符来为tableView 制作 ReusableCell。但是我在tableView propertiestable view hierarchy 中没有看到任何cell。如何在表格视图中添加单元格。

注意:基本上我想创建一个Xib 文件,该文件应包含tableView,并且tableView 应具有自定义UiTableViewCell

代码在这里:

class SuggestNearTableViewCollectionViewCell: UICollectionViewCell , UITableViewDataSource,UITableViewDelegate{

    @IBOutlet weak  var suggestTableView : UITableView!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.suggestTableView.dataSource = self
        self.suggestTableView.delegate = self

        suggestTableView.register(UINib(nibName: "SuggestNearTableViewCell", bundle: nil), forCellReuseIdentifier: "SuggestNearTableViewCell")
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell  = tableView.dequeueReusableCell(withIdentifier: "SuggestNearTableViewCell", for: indexPath) as! SuggestNearTableViewCell
         return cell
    }

}

【问题讨论】:

  • 为什么不赞成请评论
  • 在这里查看我的答案stackoverflow.com/questions/40275727/…
  • @UmairAfzal 兄弟 nib 文件有一个表格。我想要另一个 nib 文件中的称重传感器如何?
  • 所以基本上你想创建一个 Xib 文件,它应该包含一个 tableView 并且 tableView 应该有自定义的 UiTableViewCell。对吗?
  • @UmairAfzal 是的兄弟

标签: ios swift


【解决方案1】:

首先进入 File-> new -> Cocoa Touch Class 并创建 UIViewCONtroller 类。相应地为您的班级命名。

现在您将拥有一个 Xib 文件和一个 Swift 文件。 Xib 看起来像这样。

现在将 UITableView 拖放到 Xib 上,并为其设置 4 针约束,例如 top=0、bottom=0、leadin=0 和 trailing=0。现在在新创建的 swift 文件中创建 tableView 的出口。连接数据源和委托。

现在再次转到 File->New-> Coucoa Touch Class 并为 UItableViewCell 创建一个类,同时创建一个 Xib 文件,如下所示。

现在您将为您的单元格提供一个 Xib 和一个用于您的单元格的 swift 文件。只需在此 Xib 中根据需要设计您的单元格。可以说如果你想放置一个 imageView 或一个标签等。然后在你的自定义单元格的 swift 文件中创建所有组件的出口。现在将此函数添加到自定义单元格的 swift 文件中。

    class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
    let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
    tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: kYourCustomTableViewCellIdentifier)

    let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

    return cell
}

您的自定义单元格可以使用了。

现在转到 tableView 的 swift 文件,在 cellForRowAtIndexPath 中使用如下所示的这个单元格。

let cell = YourCustomTableViewCell.cellForTableView(tableView, atIndexPath: indexPath)
cell.myImageView.image = "something"
// Do something with your cell

我希望它会有所帮助。如果您发现任何困难,请告诉我。

【讨论】:

    【解决方案2】:

    viewDidLoad 中的tableViewController 中,您应该像这样注册它:

    tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: "CustomCellOne")

    cellForRowAtIndexPath 中声明单元格:

    let cell: CustomOneCell = tableView.dequeueReusableCellWithIdentifier("CustomCellOne", forIndexPath: indexPath) as! CustomOneCell
    

    如果您不在 tableViewController 内部执行此操作,那么您必须连接您的 tableView 并委托:

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
        @IBOutlet weak var tableView: UITableView!
    
        ...
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            // Table view delegate
            self.tableView.delegate = self
            self.tableView.dataSource = self
    
            ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 1970-01-01
      • 2015-07-09
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多