拉动以刷新SWIFT中的表格视图

时间:2014-12-12 23:28:03

标签: ios swift rss tableview pull-to-refresh

我对SWIFT和全面发展都很陌生。我目前正在构建RSS新闻源以磨练我(非常有限的)技能。我基本上是从随机新闻频道解析RSS提要,然后将标题存储在一个简单的表视图中。

我正在尝试使用SWIFT实现UIPullToRefresh,事实上我被告知有点卡住了!我写了以下代码。如您所见,我已将必要的代码添加到viewDidLoad方法中。此时刷新微调器运行良好,但我现在想知道在我的“刷新函数”中需要什么代码来实际刷新我的表视图。

我知道这对你们大多数人来说似乎微不足道,但我对此很新,我在过去的12个小时里一直在拉我的头发......所以在这一点上任何事情都可能有所帮助。

感谢您的帮助!

    import UIKit

class BRTableViewController: UITableViewController, NSXMLParserDelegate  {

    var parser: NSXMLParser = NSXMLParser()
    var blogPosts: [BlogPost] = []
    var postTitle: String = String()
    var postLink: String = String()
    var postDate: String = String()

    var eName: String = String()


    override func viewDidLoad() {
        super.viewDidLoad()
        let url:NSURL = NSURL(string: "http://rt.com/rss/")!
        parser = NSXMLParser(contentsOfURL: url)!
        parser.delegate = self
        parser.parse()

        // pull to refresh the table view

        refreshControl!.attributedTitle = NSAttributedString(string: "Pull to refresh")
        refreshControl!.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
        tableView.addSubview(refreshControl!)
    }


    func refresh(sender:AnyObject){
        let url:NSURL = NSURL(string: "http://rt.com/rss/")!
        parser = NSXMLParser(contentsOfURL: url)!
        parser.delegate = self
        parser.parse()
    }


        // MARK: - NSXMLParserDelegate methods

    func parser(parser: NSXMLParser!, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!) {
        eName = elementName
        if elementName == "item" {
            postTitle = String()
            postLink = String()
            postDate = String()
        }
    }

    func parser(parser: NSXMLParser!, foundCharacters string: String!) {
        let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
        if (!data.isEmpty) {
            if eName == "title" {
                postTitle += data
            } else if eName == "link" {
                postLink += data
            } else if eName == "pubDate" {
                postDate += data
            }
        }
    }

     func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
        if elementName == "item" {
            let blogPost: BlogPost = BlogPost()
            blogPost.postTitle = postTitle
            blogPost.postLink = postLink
            blogPost.postDate = postDate
            blogPosts.append(blogPost)
        }
    }

    override func parser(parser: NSXMLParser!, didEndElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!) {
        if elementName == "item" {
            self.tableView.reloadData();
            self.refreshControl.endRefreshing();
        }
    }


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

    // MARK: - Table view data source

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

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


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

        let blogPost: BlogPost = blogPosts[indexPath.row]
        cell.textLabel.text = blogPost.postTitle
        cell.detailTextLabel?.text = blogPost.postDate.substringToIndex(advance(blogPost.postDate.startIndex, 25))


        return cell
    }

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

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)  {
        if segue.identifier == "viewpost" {
            let selectedRow = tableView.indexPathForSelectedRow()?.row
            let blogPost: BlogPost = blogPosts[selectedRow!]
            let viewController = segue.destinationViewController as PostViewController
            viewController.postLink = blogPost.postLink
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这可以解决问题:

func refresh(sender:AnyObject){
    parser = NSXMLParser(contentsOfURL: url)!
    parser.delegate = self
    parser.parse()

}

这将重新解析XML并更新blogPosts变量。

删除viewDidLoad中的以下行:

refreshControl = UIRefreshControl()

您还应该覆盖parserDidEndDocument功能以包含此内容:

func parserDidEndDocument(parser: NSXMLParser!){

    self.tableView.reloadData();
    self.refreshControl?.endRefreshing();
}

一旦解析器完成,这将刷新表视图。