2015-10-30 26 views
0

我试图做自动高度来排一个RSS阅读器,但我得到这个错误:无法投类型的UITableViewCell的值设置为“ActuTblCell”

Could not cast value of type 'UITableViewCell' (0x1117bfc68) to 'package.ActuTblCell' (0x10fedca70). 

ActualitesViewController

import UIKit 

@objc 
protocol ActualitesViewControllerDelegate { 
    optional func toggleLeftPanel() 
    optional func collapseSidePanels() 
} 

class ActualitesViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, NSXMLParserDelegate { 
    @IBOutlet var tableView: UITableView! 

    var parser = NSXMLParser() 
    var posts = NSMutableArray() 
    var elements = NSMutableDictionary() 
    var element = NSString() 
    var title1 = NSMutableString() 
    var date = NSMutableString() 
    var dscrptn = NSMutableString() 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     self.navigationController?.navigationBar.barTintColor = UIColor(red: 38.0/255.0, green: 51.0/255.0, blue: 85.0/255.0, alpha: 1.0) 
     self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Gotham", size: 13)!, NSForegroundColorAttributeName : UIColor.whiteColor()] 
     self.title = "ACTUALITÉS" 

     let nib = UINib(nibName: "actuVwCell", bundle: nil) 
     tableView.registerNib(nib, forCellReuseIdentifier: "cell") 


     self.beginParsing() 
    } 

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

    func beginParsing() 
    { 
     posts = [] 
     parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://www.solutis.fr/actualites-rachat-credit,rss.html"))!)! 
     parser.delegate = self 
     parser.parse() 

     tableView!.reloadData() 
    } 

    //XMLParser Methods 

    func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) 
    { 
     element = elementName 
     if (elementName as NSString).isEqualToString("item") 
     { 
      elements = NSMutableDictionary() 
      elements = [:] 
      title1 = NSMutableString() 
      title1 = "" 
      date = NSMutableString() 
      date = "" 
      dscrptn = NSMutableString() 
      dscrptn = "" 
     } 
    } 

    func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) 
    { 
     if (elementName as NSString).isEqualToString("item") { 
      if !title1.isEqual(nil) { 
       elements.setObject(title1, forKey: "title") 
      } 
      if !date.isEqual(nil) { 
       elements.setObject(date, forKey: "date") 
      } 
      if !dscrptn.isEqual(nil) { 
       elements.setObject(dscrptn, forKey: "dscrptn") 
      } 

      posts.addObject(elements) 
     } 
    } 

    func parser(parser: NSXMLParser, foundCharacters string: String) 
    { 
     if element.isEqualToString("title") { 
      title1.appendString(string) 
     } else if element.isEqualToString("pubDate") { 
      date.appendString(string) 
     } else if element.isEqualToString("description") { 
      dscrptn.appendString(string) 
     } 
    } 

    //Tableview Methods 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return posts.count 
    } 


    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     return basicCellAtIndexPath(indexPath) 
    } 


    func basicCellAtIndexPath(indexPath:NSIndexPath) -> ActuTblCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ActuTblCell 

     setTitleForCell(cell, indexPath: indexPath) 
     setSubtitleForCell(cell, indexPath: indexPath) 
     return cell 
    } 

    func setTitleForCell(cell:ActuTblCell, indexPath:NSIndexPath) { 
     cell.titleActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("title") as! NSString as String 
    } 

    func setSubtitleForCell(cell:ActuTblCell, indexPath:NSIndexPath) { 
     cell.descriptionActuCell?.text = posts.objectAtIndex(indexPath.row).valueForKey("dscrptn") as! NSString as String 
    } 
} 

ActuTblCell

import UIKit 

class ActuTblCell: UITableViewCell { 
    @IBOutlet var titleActuCell: UILabel! 

    @IBOutlet var descriptionActuCell: UILabel! 

    @IBOutlet var dateActuCell: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 
} 

在我mainstoryboard我:

  • 我的场景类是:ActualitesViewController
  • 我的TableViewCell类:ActuTblCell

enter image description here

回答

2

你的电池类是ActuTblCell,但您注册笔尖与名actuVwCell

let nib = UINib(nibName: "actuVwCell", bundle: nil)

检查,也许问题是在这里。否则,建议根据类别命名笔尖。

+0

真布t如果我把:ActuTblCell它说:'***终止应用程序由于未捕获的异常'NSInternalInconsistencyException',原因:'无法加载捆绑NIB:'NSBundle(已加载)',名称为'ActuTblCell''' –

+0

此错误表示您没有名为ActuTblCell的nib。仔细检查错别字。 – njuri

+0

我认为这不是好的方法,笔尖是用于赢得文件没有?我的单元格在我的故事板中,在桌面视图中 –

1

你的代码有错误的位置:

let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! ActuTblCell 

你忘了设置自定义类的细胞对ActuTblCell。

Sample here

+0

我做到了:而我的TableViewCell类是:ActuTblCell –

+0

您是否在日志中得到任何警告之前,如无法在控制台中加载xxx.ActuTblCell? –

+0

我有:'***终止应用程序由于未捕获异常'NSInternalInconsistencyException',原因:'无法加载NIB捆绑:'NSBundle(加载)'名称为'ActuTblCell' ''为这一行'let nib = UINib(nibName:“ActuTblCell”,bundle:nil)' –

相关问题