拉动以刷新 ViewController 和 tableView [重复]

Posted

技术标签:

【中文标题】拉动以刷新 ViewController 和 tableView [重复]【英文标题】:Pull to Refresh on ViewController and tableView [duplicate] 【发布时间】:2017-07-27 07:53:01 【问题描述】:

我想拉刷新我的 RSS 提要新闻应用程序。我使用视图控制器和UITableView。在视图控制器上我应该怎么做?我们应该添加什么?我的代码在这里。

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,XMLParserDelegate  
@IBOutlet weak var tblView: UITableView!

var parser = XMLParser()
var news = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var link = NSMutableString()
var desc = NSMutableString()


override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    parsingDataFromURL()
    addNavBarImage()



override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()






func parsingDataFromURL()

    news = []
    parser = XMLParser(contentsOf: URL(string: "feedurl")!)!
    parser.delegate = self
    parser.parse()
    tblView.reloadData()



func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) 

    element = elementName as NSString

    if(elementName as NSString) .isEqual(to: "item")
    
        elements = NSMutableDictionary()
        elements = [:]
        title1 = NSMutableString()
        title1 = " "
        link = NSMutableString()
        link = " "
    


func parser(_ parser: XMLParser, foundCharacters string: String) 

    if element .isEqual(to: "title")
    
        title1.append(string)
    
    else if element.isEqual(to: "description")
    
        desc.append(string)
    
    else if element.isEqual(to: "link")
    
        link.append(string)
    



func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) 
    if (elementName as NSString) .isEqual(to: "item")
    
        if !title1 .isEqual(nil)

        
            elements.setObject(title1, forKey: "title" as NSCopying)
        

        if !link .isEqual(nil)
        
            elements.setObject(link, forKey: "link" as NSCopying)
        
        if !desc.isEqual(nil)
        
            elements.setObject(desc, forKey: "description" as NSCopying)
        


        news.add(elements)
    



internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int

    return news.count


internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

    var cell = tableView.dequeueReusableCell(withIdentifier: "myCell")! as UITableViewCell

    if (cell.isEqual(NSNull.self))
    
        cell = Bundle.main.loadNibNamed("myCell", owner: self, options: nil)?[0] as! UITableViewCell
    

    cell.textLabel?.text = (news.object(at: indexPath.row) as AnyObject).value(forKey: "title") as! NSString as String

    cell.detailTextLabel?.text = (news.object(at: indexPath.row) as AnyObject).value(forKey: "link") as! NSString as String

    return cell


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)


    tableView.deselectRow(at: indexPath, animated: true)
    let newsdetailVC = storyboard?.instantiateViewController(withIdentifier: "NewsDetailViewController") as! NewsDetailViewController
    newsdetailVC.selectedTitle = (news.object(at: indexPath.row) as AnyObject).value(forKey: "title") as! NSString as String
    newsdetailVC.selectedUrl   = (news.object(at: indexPath.row) as AnyObject).value(forKey: "link") as! NSString as String

    self.navigationController?.pushViewController(newsdetailVC, animated: true)


【问题讨论】:

可以实现UIScrollViewDelegate方法scrollViewDidScroll,如果contentOffset.y大于某个阈值,则刷新表格视图 你的 UIViewController 是否只包含一个 UITableView 而没有任何其他元素? 【参考方案1】:

如果我没听错的话,你想在加载完所有数据后刷新表格视图。如果是这种情况,您需要parserDidEndDocument

func parserDidEndDocument(parser: NSXMLParser)
    table?.reloadData()

您可以查看this 和/或this 以了解拉动刷新

【讨论】:

不,我想拉刷新。 我的回答中有链接,可能是你想要的。【参考方案2】:

试试这个 编辑

var refreshControl: UIRefreshControl!

override func viewDidLoad() 
   super.viewDidLoad()

   refreshControl = UIRefreshControl()
   refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
   refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
   tableView.addSubview(refreshControl) // not required when using UITableViewController


func refresh(sender:AnyObject) 
   // Code to refresh table view  
      parsingDataFromURL()
      tblView.reloadData()

【讨论】:

刷新表格视图的代码是什么? 是的!不行吗? 是的,不工作。

以上是关于拉动以刷新 ViewController 和 tableView [重复]的主要内容,如果未能解决你的问题,请参考以下文章

工作拉动以禁用反弹刷新

拉动以刷新 tableviewcontroller 负载

在 TableView 标题下放置拉动以刷新控件

RefreshControl 数据重复每次在 React Native 中拉动以刷新 ScrollView

JQuery Mobile:拉动以刷新列表视图[重复]

重置分页以重新获取拉动时的默认变量以刷新抛出光标警告?