简体   繁体   中英

How to convert a xCode xib based view controller to a storyboard view controller?

I was following a Swift tutorial which use a xib file as view controller and was trying to get it into the storyboard. I use xCode7 and Swift 2.1 I get some problem for the codes below in particular:

var cell:CatsTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CatsTableViewCell

        if(cell == nil) {
            cell = NSBundle.mainBundle().loadNibNamed("CatsTableViewCell", owner: self, options: nil)[0] as? CatsTableViewCell
        }

How can I translate this one above into the one to use in the project with the storyboard?

I got some problem here below as well. I start using xCode not a long time ago and I am a little bit confused about the meaning of the code below. I've tried to google it but I have just got very detailed explanation hard to understand as a beginner.

required init(coder aDecoder:NSCoder)
    {
        fatalError("NSCoding not supported")  
    }

Here below you can find the whole code of the view controller and the link to the full tutorial. Hope somebody can help me, please. Thanks in advance.

http://www.appcoda.com/instagram-app-parse-swift/

//
//  CatsTableViewController.swift
//  Paws
//
//  Created by Simon Ng on 15/4/15.
//  Copyright (c) 2015 AppCoda. All rights reserved.
//

import UIKit

class CatsTableViewController: PFQueryTableViewController {


    let cellIdentifier:String = "CatCell"

    override func viewDidLoad() {


     tableView.registerNib(UINib(nibName: "CatsTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)

        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func queryForTable() -> PFQuery {
        let query:PFQuery = PFQuery(className:self.parseClassName!)


        if(objects?.count == 0)
        {
            query.cachePolicy = PFCachePolicy.CacheThenNetwork
        }

        query.orderByAscending("name")

        return query

    }

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


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        var cell:CatsTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CatsTableViewCell

        if(cell == nil) {
            cell = NSBundle.mainBundle().loadNibNamed("CatsTableViewCell", owner: self, options: nil)[0] as? CatsTableViewCell
        }

        cell?.parseObject = object

        if let pfObject = object {
            cell?.catNameLabel?.text = pfObject["name"] as? String

            var votes:Int? = pfObject["votes"] as? Int
            if votes == nil {
                votes = 0
            }
            cell?.catVotesLabel?.text = "\(votes!) votes"

            var credit:String? = pfObject["cc_by"] as? String
            if credit != nil {
                cell?.catCreditLabel?.text = "\(credit!) / CC 2.0"
            }

            cell?.catImageView?.image = nil
            if var urlString:String? = pfObject["url"] as? String {
                var url:NSURL? = NSURL(string: urlString!)

                if var url:NSURL? = NSURL(string: urlString!) {
                    var error:NSError?
                    var request:NSURLRequest = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 5.0)

                    NSOperationQueue.mainQueue().cancelAllOperations()

                    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {
                        (response:NSURLResponse?, imageData:NSData?, error:NSError?) -> Void in

                        cell?.catImageView?.image = UIImage(data: imageData!)

                    })
                }
            }

        }

        return cell
    }

    override init(style: UITableViewStyle, className: String!)
    {
        super.init(style: style, className: className)

        self.pullToRefreshEnabled = true
        self.paginationEnabled = false
        self.objectsPerPage = 25

        self.parseClassName = className

        self.tableView.rowHeight = 350
        self.tableView.allowsSelection = false
    }

    required init(coder aDecoder:NSCoder)
    {
        fatalError("NSCoding not supported")  
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Change below code

 var cell:CatsTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CatsTableViewCell

    if(cell == nil) {
        cell = NSBundle.mainBundle().loadNibNamed("CatsTableViewCell", owner: self, options: nil)[0] as? CatsTableViewCell
    }

to

 let cell = tableView.dequeueReusableCellWithIdentifier("CatsTableViewCell", forIndexPath: indexPath) as! CatsTableViewCell

And for "required" keyword check link What does the "required" keyword in Swift mean?

Your viewController should have a property for the tableview, like this:

@IBOutlet var tableview : UITableView?

Then in your storyboard, connect the tableview to that outlet (tutorials on connecting outlets abound, or let me know if you get stuck). Then, in viewDidLoad, call the same method you have above: self.tableView.registerNib etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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