swift2 在Swift中将自定义UITableViewCell从nib加载到UIViewController

sirbozc5  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(235)

我目前正在开发一个exercise swift程序,该程序要求将CoreData结果显示在表格上。
我已经将我的应用程序结构化为故事板本身不包含任何UI元素,只包含视图(附带一个UIViewController类),然后加载自定义nib/xib(也附带一个UIView子类)。在本例中,xib包含一个UITableView,另一个xib包含单元格。

表视图类

import UIKit

protocol SkillsViewDelegate{
}

class SkillsView: UIView {
    var delegate : SkillsViewDelegate!
    @IBOutlet weak var skillsTable: UITableView!
}

表视图控制器

import UIKit
import CoreData

class SkillsViewController: UIViewController, SkillsViewDelegate, UITableViewDataSource, UITableViewDelegate {
    var displaySkillsView : SkillsView!
    var displaySkillsCell : SkillViewCell!
    let textCellIdentifier = "skillViewCell"

    override func viewDidLoad() {
        super.viewDidLoad()

        self.displaySkillsView = UIView.loadFromNibNamed("SkillsView") as! SkillsView
        self.displaySkillsView.delegate = self
        self.view = self.displaySkillsView
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK : -Table view delegates

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

        return cell
    }
}

表格单元格视图类

import UIKit

protocol SkillsViewCellDelegate{
}

class SkillViewCell: UITableViewCell {
    var delegate : SkillsViewCellDelegate!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
   }

    @IBOutlet weak var skillName: UILabel!
    @IBOutlet weak var skillRank: UILabel!
}

我不知道如何将cell xib调用到控制器中。我猜它是like this problem,但不同的是这个家伙使用的是UITableViewController。我也试着添加它,但我得到了一个Multiple inheritance from classes 'UIViewController' and 'UITableViewController'

我的尝试

我试着把UITableViewController加到上面,但是我得到了一个Multiple inheritance from classes 'UIViewController' and 'UITableViewController'来解决我的麻烦。
有什么建议吗?

e1xvtsh3

e1xvtsh31#

您可以在您的SkillsViewController中注册您的SkillsViewCell

对象C

-(void)viewDidLoad {
   [super viewDidLoad];
   UINib *cellNib = [UINib nibWithNibName:@"NibName" bundle:nil];
   [self.skillsTable registerNib:cellNib forCellReuseIdentifier:textCellIdentifier];
}

雨燕2

override func viewDidLoad() {
   super.viewDidLoad()
   let nibName = UINib(nibName: "NibName", bundle:nil)
   self.skillsTable.registerNib(nibName, forCellReuseIdentifier: textCellIdentifier)
}

雨燕4

override func viewDidLoad() {
   super.viewDidLoad()
   let nibName = UINib(nibName: "NibName", bundle:nil)
   self.skillsTable.register(nibName, forCellReuseIdentifier: textCellIdentifier)
}

则仅继承自UIViewController而非UITableViewController
若要呼叫自订储存格特定的方法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! SkillViewCell

    cell.skillName.text = "Some text"
    cell.skillRank.text = "Some text"
    return cell
}
ni65a41a

ni65a41a2#

我想你需要使用

func registerNib(_ nib: UINib?, forCellReuseIdentifier identifier: String)

单位为viewDidLoad
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instm/UITableView/registerNib:forCellReuseIdentifier:

相关问题