How to load Custom cell (Xib) in UITableview using

2019-06-14 20:35发布

Here's my code

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        
}

I have tried with the above code and nothing is displayed in Tableview, please help me to change this code working

Thanks

9条回答
叛逆
2楼-- · 2019-06-14 21:03

My code:

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
}
查看更多
一夜七次
3楼-- · 2019-06-14 21:04

In viewDidLoad register XIB as shown below

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
查看更多
手持菜刀,她持情操
4楼-- · 2019-06-14 21:09

if you would like to Load custom cell(Xib) in UITableView in swift ,you may use the following code.

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
}

Don’t forget to register nib in viewDidLoad

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.
}
查看更多
Lonely孤独者°
5楼-- · 2019-06-14 21:10
  1. Register your NIB with the reuse identifier:

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "Cell")
    }
    
  2. Your cellForRowAtIndexPath would then instantiate the cell.

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! devicedet
    
        return cell
    }
    
查看更多
We Are One
6楼-- · 2019-06-14 21:11
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 
        }

Added Screenshot for setting Datasource and delegate from storyboard enter image description here

查看更多
贼婆χ
7楼-- · 2019-06-14 21:17

Your fuction registerNib , you try to write in viewDidLoad() method and remove from cellForRowAtIndexPah and then test your project

Your Cell Instance should be of CustomCell instance and not of UITableView Cell. var cell :UITableViewCell should be replaced by var cell :devicedet

查看更多
登录 后发表回答