简体   繁体   English

在Swift中将自定义UITableViewCell从笔尖加载到UIViewController

[英]Loading a Custom UITableViewCell from nib to a UIViewController in Swift

I'm currently working on an exercise swift program, one that requires CoreData results to be displayed on a table. 我目前正在研究一项运动快速程序,该程序要求CoreData结果显示在表上。

I've structured my app such that the storyboard itself doesn't contain any UI elements, only views (with an accompanying UIViewController class), which then loads custom nibs / xibs (also accompanied by a UIView Subclass). 我已经对应用程序进行了结构设计,使得故事板本身不包含任何UI元素,仅包含视图(带有随附的UIViewController类),然后加载自定义笔尖/ xibs(还附带UIView子类)。 In this case, the xib contains a UITableView , and a separate xib contains the cell. 在这种情况下,xib包含UITableView ,而另一个xib包含单元格。

TableView Class : TableView类别

import UIKit

protocol SkillsViewDelegate{
}

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

TableView Controller : 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 Class : 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!
}

I can't figure out how to call the cell xib into the controller. 我不知道如何将单元xib调用到控制器中。 I would guess it's something like this problem , but the difference is the guy is using a UITableViewController . 我想这是类似的问题 ,但是不同的是那个家伙正在使用UITableViewController I tried adding that as well, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles. 我也尝试添加它,但是由于麻烦,我Multiple inheritance from classes 'UIViewController' and 'UITableViewController'获得了Multiple inheritance from classes 'UIViewController' and 'UITableViewController'

What I Tried : 我试过的
I tried adding UITableViewController up top, but I get a Multiple inheritance from classes 'UIViewController' and 'UITableViewController' for my troubles. 我尝试在顶部添加UITableViewController ,但是由于麻烦,我Multiple inheritance from classes 'UIViewController' and 'UITableViewController'获得了Multiple inheritance from classes 'UIViewController' and 'UITableViewController'

Any suggestions? 有什么建议么?

You can just register your SkillsViewCell in your SkillsViewController 您可以在SkillsViewController中注册SkillsViewCell

Obj C 对象C

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

Swift 2 迅捷2

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

Swift 4 斯威夫特4

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

Then only inherit from UIViewController not UITableViewController 然后仅继承自UIViewController而不继承自UITableViewController

To call methods specific to your custom cell 调用特定于您的自定义单元格的方法

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM