我正在关注一个Swift教程,该教程使用xib文件作为视图控制器,并试图将其放入故事板。我使用xCode7和Swift 2.1我特别为下面的代码遇到了一些问题:
var cell:CatsTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CatsTableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("CatsTableViewCell", owner: self, options: nil)[0] as? CatsTableViewCell
}
如何将上面的这一个翻译成在故事板项目中使用的那个?
我在下面也遇到了一些问题。我很久以前就开始使用xCode了,我对下面代码的含义有点困惑。我试图谷歌它,但我有一个非常详细的解释难以理解为初学者。
required init(coder aDecoder:NSCoder)
{
fatalError("NSCoding not supported")
}
在下面,您可以找到视图控制器的完整代码以及完整教程的链接。希望有人可以帮助我。提前谢谢。
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.
}
*/
}
答案 0 :(得分:0)
更改以下代码
var cell:CatsTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CatsTableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("CatsTableViewCell", owner: self, options: nil)[0] as? CatsTableViewCell
}
到
let cell = tableView.dequeueReusableCellWithIdentifier("CatsTableViewCell", forIndexPath: indexPath) as! CatsTableViewCell
对于“必需”关键字检查链接What does the "required" keyword in Swift mean?
答案 1 :(得分:0)
您的viewController应该具有tableview的属性,如下所示:
@IBOutlet var tableview : UITableView?
然后在你的故事板中,将tableview连接到那个插座(有关连接插座的教程比比皆是,或者让我知道你是否卡住了)。然后,在viewDidLoad中,调用上面的相同方法:self.tableView.registerNib等。