TableView在解析XML时显示空白单元格

时间:2015-02-14 00:18:04

标签: ios uitableview swift

我使用TableViewController在我的XML Feed阅读器上工作,但它工作得很好,但由于它不灵活,我想转移到另一个ViewController,其中包括`TableView' 。但是,即使我认为我的连接正确(从tableview创建IBOutlet,在CustomCell类中使用自定义单元格中的正确对象等),它也会显示空白单元格。

您可以在下面找到代码。我正在使用CustomCell类来创建我的自定义单元格。我在这里缺少什么?

class originalViewController: UIViewController,UITableViewDelegate, NSXMLParserDelegate, UIScrollViewDelegate{

@IBOutlet var tableView: UITableView!

var parser = NSXMLParser()
var feeds = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var ftitle = NSMutableString?()
var link = NSMutableString?()
var fdescription = NSMutableString?()
var fIMG = NSMutableString?()
var fAuthor = NSMutableString?()
var fDate = NSMutableString?()


override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")


    feeds = []
    var url = NSURL(string: "http://www.marketoloji.com/?feed=rss2")
    parser = NSXMLParser(contentsOfURL: url)!
    parser.delegate = self
    parser.shouldProcessNamespaces = false
    parser.shouldReportNamespacePrefixes = false
    parser.shouldResolveExternalEntities = false
    parser.parse()
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "openPage" {

        var indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow()!
        let wvc: WebViewController = segue.destinationViewController as WebViewController
        var selectedURL: String = feeds[indexPath.row].objectForKey("link") as String
        selectedURL = selectedURL.stringByReplacingOccurrencesOfString(" ", withString: "")
        selectedURL = selectedURL.stringByReplacingOccurrencesOfString("\n", withString: "")
        wvc.selectedLink = selectedURL
    }
}


func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) {
    element = elementName

    if (element as NSString).isEqualToString("item"){
        elements = NSMutableDictionary.alloc()
        elements = [:]
        ftitle = ""
        link = ""
        fdescription = ""
        fAuthor = ""
        fDate = ""
        fIMG = ""

    } else if element.isEqualToString("enclosure") {
        var imgLink = attributeDict["url"] as String
        imgLink = imgLink.stringByReplacingOccurrencesOfString(" ", withString: "")
        imgLink = imgLink.stringByReplacingOccurrencesOfString("\n", withString: "")

        fIMG?.appendString(imgLink)
        println(imgLink)
    }
}

func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!){

    if (elementName as NSString).isEqualToString("item"){
        if ftitle != nil {
            elements.setObject(ftitle!, forKey: "title")
        }

        if link != nil {
            elements.setObject(link!, forKey: "link")
        }

        if fdescription != nil {
            elements.setObject(fdescription!, forKey: "description")
        }

        if fAuthor != nil {
            elements.setObject(fAuthor!, forKey: "creator")
        }

        if fDate != nil {
            elements.setObject(fDate!, forKey: "date")
        }

        if fIMG != nil {
            elements.setObject(fIMG!, forKey: "imageLink")
        }

        feeds.addObject(elements)
    }

}

func parser(parser: NSXMLParser!, foundCharacters string: String!) {

    if element.isEqualToString("title") {
        ftitle?.appendString(string)
    } else if element.isEqualToString("link") {
        link?.appendString(string)
    } else if element.isEqualToString("description") {
        fdescription?.appendString(string)
    } else if element.isEqualToString("dc:creator") {
        fAuthor?.appendString(string)
    } else if element.isEqualToString("pubDate") {
        fDate?.appendString(string)
    }

}

func parserDidEndDocument(parser: NSXMLParser!) {
    self.tableView.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 20
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    //let cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell
    let cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("Cell") as CustomCell

    cell.setCell(feeds.objectAtIndex(indexPath.row).objectForKey("title") as String, fDescription: feeds.objectAtIndex(indexPath.row).objectForKey("description") as String, fAuthor: feeds.objectAtIndex(indexPath.row).objectForKey("creator") as String, fDate: feeds.objectAtIndex(indexPath.row).objectForKey("date") as String, fImage: feeds.objectAtIndex(indexPath.row).objectForKey("imageLink") as String)




    //cell.backgroundColor = UIColor.clearColor()

    return cell
}

}

1 个答案:

答案 0 :(得分:0)

想通了我没有将表的委托和数据源连接到视图控制器,这是主要问题。