使用 xib 和 tableView 的多个视图

Posted

技术标签:

【中文标题】使用 xib 和 tableView 的多个视图【英文标题】:Multiple views using xib and tableView 【发布时间】: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 时有某种我不知道的规则?

【问题讨论】:

【参考方案1】:

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

【讨论】:

伙计...我无话可说如何感谢你。我希望代码之神能够保护您的安全。干杯伙伴!

以上是关于使用 xib 和 tableView 的多个视图的主要内容,如果未能解决你的问题,请参考以下文章

使用 Swift 3.0 中的数据在 Xib 中显示 tableView

多个控制器使用一个 xib

使用 Xib 在 Tableview Header 中快速重新加载 CollectionView

将两个 xib 文件中的两个 tableviews 添加到主 xib 视图

如何使用 iOS 故事板创建具有两个嵌套 tableview 单元格 xib 的单个 tableview?

iOS8 Swift 无法使用 xib 创建视图控制器