【发布时间】:2016-06-11 20:29:16
【问题描述】:
我在使用 .xib 时遇到了 Xcode 和自定义 TableView 的问题。看看吧。
主控制器包含三个按钮,连接三个独立的视图。
.xib 文件名为“vwTblCell”,包含一个名为“cell”的自定义表格视图单元格。
TblCell.swift //带有 Nib 方法的类
import UIKit
class TblCell: UITableViewCell {
@IBOutlet weak var imgJedzName: UIImageView! //Connected from .xib file
@IBOutlet weak var lblJedzName: UILabel! //Connected from .xib file
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
第一个视图控制器
import UIKit
class SniadanieViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var tableData = ["Kanapeczka", "Jablko", "Jajecznica"]
override func viewDidLoad() {
super.viewDidLoad()
self.title="Sniadanie"
// Register custom cell
let nib = UINib(nibName: "vwTblCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
cell.lblJedzName.text = tableData[indexPath.row]
cell.imgJedzName.image = UIImage(named: tableData[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("Row \(indexPath.row) selected")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
有效! - Working view
第二个视图控制器 - 只有 self.title 与第一个控制器不同
import UIKit
class ObiadViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var tableData = ["Kanapeczka", "Jablko", "Jajecznica"]
override func viewDidLoad() {
super.viewDidLoad()
self.title="Obiad"
// Register custom cell
let nib = UINib(nibName: "vwTblCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "cell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell
cell.lblJedzName.text = tableData[indexPath.row]
cell.imgJedzName.image = UIImage(named: tableData[indexPath.row])
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("Row \(indexPath.row) selected")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
出现空tableView :/ I shouldn't be empty
第三视图控制器
import UIKit
class KolacjaViewController: UIViewController, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
self.title="Kolacja"
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel!.text = "Row number \(indexPath.row)"
cell.backgroundColor = UIColor.blueColor()
return cell
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("Row \(indexPath.row) selected")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}}
出现空 tableView - 与第二个控制器相同。你必须相信我,我希望我可以添加更多图片,但由于
为什么第二个和第三个表格视图控制器不起作用?代码看起来不错,我猜在使用 .xib 时有某种我不知道的规则?
【问题讨论】:
标签: ios xcode swift tableview xib