【问题标题】:How to load Custom cell (Xib) in UITableview using Swift如何使用 Swift 在 UITableview 中加载自定义单元格(Xib)
【发布时间】:2015-04-10 13:17:41
【问题描述】:

这是我的代码

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{
    return 10   
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell!

    bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
    //NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell

    cell = blucell
    devname.text = peri[indexPath.row]
    devstrength.text = signalstrength[indexPath.row]

    bluetoothlog.backgroundColor = UIColor.clearColor()
    return cell        
}

我已尝试使用上述代码,但 Tableview 中没有显示任何内容,请帮助我更改此代码的工作方式

谢谢

【问题讨论】:

  • 请重新格式化您的代码,atm 难以阅读!

标签: ios iphone uitableview swift xcode6


【解决方案1】:
  1. 使用重用标识符注册您的 NIB:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "Cell")
    }
    
  2. 然后您的 cellForRowAtIndexPath 将实例化该单元格。

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

【讨论】:

    【解决方案2】:
    • 命令 registerNib 应该在 ViewDidLoad 中调用,而不是在每个对行的单元格调用中
    • 什么是'bluecell'?在您的回调 cellforTable.. 中,您分配 cell = blucell -> 这可能会导致您的问题。

    试试这个:

    import UIKit
    
    class YourViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
    
    override func viewDidLoad() {
            super.viewDidLoad()
    
    //call register nib here!!
            bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
        }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    {
        return peri.count    
    }
    
    //it seem that you have to add a è
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell!
    {
        let cell :UITableViewCell = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell!
    /*  REMOVE THIS
        bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
        //NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell
    */
      //// THEN, UPDATE YOUR CELL with YOUR DATAs
    
    /* I don't understand what are you doing with this line of code:
        cell = blucell
    
    */
    
        devname.text = peri[indexPath.row]
        devstrength.text = signalstrength[indexPath.row]
    
    //Seem that bluetoothlog is the tableview, you should call it in viewdidload or viewwillappear()
        bluetoothlog.backgroundColor = UIColor.clearColor()
    
        return cell        
    }
    
    }
    

    【讨论】:

      【解决方案3】:
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
      {
      
          var cell :devicedet = tableView.dequeueReusableCellWithIdentifier("discovered") as UITableViewCell!
      
          bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered")
          //NSBundle.mainBundle().loadNibNamed("devicedet", owner: nil, options: nil)[0] as UITableViewCell
      
          cell = blucell
          devname.text = peri[indexPath.row]
          devstrength.text = signalstrength[indexPath.row]
      
          bluetoothlog.backgroundColor = UIColor.clearColor()
          return cell        
      }
      

      【讨论】:

      • 如果仍然没有得到所需的输出。然后在 ViewDidLoad() 中添加您的 bluetoothlog.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "discovered") 并从当前方法中删除。
      • 然后尝试使用您的代码....以 NSException 类型的未捕获异常终止,此类与键 blucell 的键值编码不兼容。'
      • customcell .h 和 .m 文件名的名称是什么。创建该类的实例。
      • 蓝牙 ViewController1.swift
      • 你为什么不在你的其他答案下发布代码?
      【解决方案4】:

      在viewDidLoad寄存器XIB中如下图

      var userDetailsNIB = UINib(nibName: "UserDetailsCell", bundle: nil)
              viewListingTable.registerNib(userDetailsNIB, forCellReuseIdentifier: "UserDetailsViewCellID")
      
      And in func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
      
      function use it by access it with Identifier which created in viewDidLoad method
      
      let cell = tableView.dequeueReusableCellWithIdentifier("UserDetailsViewCellID") as UserDetailsViewCell
      
      return cell
      

      【讨论】:

        【解决方案5】:

        如果您想在 UITableView 中快速加载自定义单元格(Xib),您可以使用以下代码。

        func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return array.count; //This function returns number of rows in table view
        }
        
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            var cell:YourTableViewCell! = tableView.dequeueReusableCellWithIdentifier("YourTableViewCell", forIndexPath:indexPath)as! YourTableViewCell
           // Do additional code here
        }
        

        别忘了在 viewDidLoad 中注册 nib

        override func viewDidLoad() {
            super.viewDidLoad()
            // registering your nib
            let yourNibName = UINib(nibName: "YourTableViewCell", bundle: nil)
            tableView.registerNib(yourNibName, forCellReuseIdentifier: "YourTableViewCell")
            // Do any additional setup after loading the view.
        }
        

        【讨论】:

          【解决方案6】:

          在 Swift 中注册 xib Cell

          override func viewDidLoad() {
              super.viewDidLoad()
              tableView.register(UINib(nibName: "XibCell", bundle: nil), forCellReuseIdentifier: "Cell")
          }
          
          
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! UITableViewCell
              return cell
          }
          

          【讨论】:

            【解决方案7】:

            我的代码:

            override func viewDidLoad() {
                super.viewDidLoad()
                 tableView.register(
                     UINib(nibName: "XibCell", bundle: nil), 
                     forCellReuseIdentifier: "Cell"
                 )
             }
            
            func tableView(_ tableView: UITableView, 
                           cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
                let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") 
                return cell as! UITableViewCell
            }
            

            【讨论】:

              【解决方案8】:
              import UIKit
                  class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
              
                  //MARK:- Sample Data Array to load in TableView
                      let sampleArrray: \[String\] = \["val1", "val2", "val3", "val4", "val5"]
              
                  //MARK:- Cell reuse identifier 
                      let cellReuseIdentifier = "cell"
              
              
                  //MARK:- UITableView outlet 
                       @IBOutlet var tableView: UITableView!
              
                        override func viewDidLoad() {
                          super.viewDidLoad()
              
                          // Registering the custom cell
              
                          self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
              
                          // Setting delegate and Datasourse as same viewcontroller (this code is not neccessory if we are setting it from storyboard)
              
                          tableView.delegate = self
                          tableView.dataSource = self
                      }
                  //UITableview required methods 
              
                      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                          return sampleArrray.count
                      }
              
                      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                          let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as UITableViewCell!
              
                          cell.textLabel?.text = sampleArrray\[indexPath.row\]
              
                          return cell
                      }
              
                      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
                         print("clicked at: \(indexPath.row)")
                         //Here u can get the selected cell and add action related to this cell seelction 
                      }
              

              添加了用于从情节提要设置数据源和委托的屏幕截图

              【讨论】:

                【解决方案9】:

                您的功能 registerNib ,您尝试在 viewDidLoad() 方法中写入并从 cellForRowAtIndexPah 中删除,然后测试您的项目

                您的 Cell 实例应该是 CustomCell 实例而不是 UITableView Cell。 var cell :UITableViewCell 应替换为 var cell :devicedet

                【讨论】:

                • 如果不注册单元格,应用将在加载视图时崩溃
                猜你喜欢
                • 2016-09-15
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-02-13
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多