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条回答
小情绪 Triste *
2楼-- · 2019-06-14 21:23
  • the command registerNib should be called in ViewDidLoad, but not in each call of cell for row
  • what is 'bluecell'? In your call-back cellforTable.., you assign cell = blucell -> this can cause your problems.

Try this:

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楼-- · 2019-06-14 21:23
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        
}
查看更多
放荡不羁爱自由
4楼-- · 2019-06-14 21:27

Register xib Cell in Swift

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
}
查看更多
登录 后发表回答