Ios 如何使用Swift在UITableview中加载自定义单元格(Xib)
这是我的密码Ios 如何使用Swift在UITableview中加载自定义单元格(Xib),ios,iphone,uitableview,swift,xcode6,Ios,Iphone,Uitableview,Swift,Xcode6,这是我的密码 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell :UITableViewC
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中未显示任何内容,请帮助我更改此代码
感谢您的功能注册表nb,您尝试使用viewDidLoad方法编写并从cellforrowatinexpah中删除,然后测试您的项目 您的单元格实例应为CustomCell实例,而不是UITableView单元格。 变量单元格:UITableViewCell应替换为 var单元:DeviceNet
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中调用registerNib命令,但不应在行的每个单元格调用中调用该命令
什么是“蓝细胞”?在回拨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
}
}
添加了用于从故事板设置数据源和委托的屏幕截图
在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
如果要在swift的UITableView中加载自定义cellXib,可以使用以下代码
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.
}
使用重用标识符注册NIB:
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "devicedet", bundle: nil), forCellReuseIdentifier: "Cell")
}
然后,cellForRowAtIndexPath将实例化该单元格
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! devicedet
return cell
}
在Swift中注册xib单元
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
}
我的代码:
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
}
请重新格式化您的代码,因为atm很难读取!如果仍然无法获得所需的输出。然后添加在ViewDidLoad中发现的bluetoothlog.RegisterNibInbInbName:DeviceSet、bundle:nil、forCellReuseIdentifier:并从当前方法中删除。然后尝试使用代码。。。。以NSException类型的未捕获异常终止此类不符合密钥blucell的键值编码。“customcell.h和.m文件名的名称是什么?”。创建该类的实例。Bluetooth ViewController1.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")
return cell as! UITableViewCell
}