当前位置:  开发笔记 > IOS > 正文

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

如何解决《将自定义UITableViewCell从nib加载到Swift中的UIViewController》经验,为你挑选了1个好方法。

我目前正在开发一个练习swift程序,需要将CoreData结果显示在表格上.

我构建了我的应用程序,使得故事板本身不包含任何UI元素,只包含视图(带有附带的UIViewController类),然后加载自定义的nibs/xibs(也附带一个UIView子类).在这种情况下,xib包含a UITableView,单独的xib包含单元格.

TableView类:

import UIKit

protocol SkillsViewDelegate{
}

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

TableView控制器:

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
    }
}

TableCellView类:

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!
}

我无法弄清楚如何将单元格xib调用到控制器中.我猜它就像这个问题,但区别在于这家伙正在使用UITableViewController.我尝试添加它,但我找到了Multiple inheritance from classes 'UIViewController' and 'UITableViewController'麻烦.

我尝试了什么:
我尝试加上UITableViewController顶部,但我找到了Multiple inheritance from classes 'UIViewController' and 'UITableViewController'麻烦.

有什么建议?



1> Scott Lewis..:

您可以在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
}

推荐阅读
我我檬檬我我186
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有