使用xib和tableView的多个视图

时间:2016-06-11 20:29:16

标签: ios xcode swift tableview xib

我使用.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 - 与第二个控制器中的相同。你必须相信我,我希望我可以添加更多的图片,但由于< 10点声望点我不能添加超过2张图片。

为什么第二个和第三个表视图控制器不工作?代码看起来很好,我想在使用我不了解的.xib时会有某种规则吗?

1 个答案:

答案 0 :(得分:0)

您需要设置数据源:tableView.dataSource = self,我在您发布的代码中看不到。

相关问题