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

时间:2015-12-12 06:35:25

标签: ios swift uitableview swift2

我目前正在制作一个练习快速程序,需要将CoreData结果显示在桌面上。

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

TableView类

UITableView

TableView控制器

import UIKit

protocol SkillsViewDelegate{
}

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

TableCellView类

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

我无法弄清楚如何将单元格xib调用到控制器中。我猜它是like this problem的东西,但区别在于那个人正在使用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! } 。我也尝试添加它,但我遇到了UITableViewController我的麻烦。

我尝试了什么
我尝试将Multiple inheritance from classes 'UIViewController' and 'UITableViewController'添加到最顶层,但我遇到了问题的UITableViewController

有什么建议吗?

2 个答案:

答案 0 :(得分:5)

您可以在SkillsViewController中注册SkillsViewCell

对象C

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

Swift 2

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

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

答案 1 :(得分:1)

我认为你需要使用

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