将数据从TableViewController传递到解析器

时间:2015-09-24 14:25:08

标签: ios uitableview parsing xml-parsing parameter-passing

在我的应用开发期间,我遇到了一个特定的问题。 我有一个包含以下列的数据库:Dossier_Title,Dossier_Name,Dossier_Category和Dossier_Description。每个档案的描述都是独一无二的。

  1. 首先,我正在调用我的WebService,它选择唯一的Dossier_Title并形成一个xml页面。之后,该应用程序解析此页面并在表格视图中形成许多唯一单元格。它还形成一个由Dossier_Title和Dossier_category组成的数组。
  2. 现在我想构建一个新的tableView,它包含Dossier_Name和Dossier_Description,它基于我在前一步骤中实现的Dossier_Category。为此,我想调用一个新的WebService并使用该Category作为条件进行解析。
  3. 我的问题是:如何将Dossier_category传递给我的第二个解析器,以便将其作为条件使用? 这是我的第一个解析器代码,我假设第二个解析器代码与添加一些新条件几乎相同

    class DossierParser: NSObject, NSXMLParserDelegate {
    
    
        var parser = NSXMLParser()
        var feeds = NSMutableArray()
        var elements = NSMutableDictionary()
        var element = NSString()
        var ftitle = NSMutableString()
        var link = NSMutableString()
        var fdescription = NSMutableString()
        var fdate = NSMutableString()
        var fcategory = NSMutableString()
    
        // initilise parser
        /*
        func initWithURL(url :NSURL) -> AnyObject {
        startParse(url)
        return self
        }
        */
        init(URL: NSURL){
            super.init()
            startParse(URL)
        }
    
    
    
        func startParse(url :NSURL) {
            feeds = []
            parser = NSXMLParser(contentsOfURL: url)!
            parser.delegate = self
            parser.shouldProcessNamespaces = false
            parser.shouldReportNamespacePrefixes = false
            parser.shouldResolveExternalEntities = false
            parser.parse()
        }
    
        func allFeeds() -> NSMutableArray {
            return feeds
        }
        func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
    
            self.element = elementName
    
            if self.element == "News" {
                elements = NSMutableDictionary()
                elements = [:]
                ftitle = NSMutableString()
                ftitle = ""
                fdescription = NSMutableString()
                fdescription = ""
                fdate = NSMutableString()
                fdate = ""
                fcategory = NSMutableString()
                fcategory = ""
    
            }
        }
    
        func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
    
            if (elementName as NSString).isEqualToString("News") {
                if ftitle != "" {
                    elements.setObject(ftitle, forKey: "DosNum")
                }
                if fcategory != "" {
                    elements.setObject(fcategory, forKey: "Dossier_number")
                }
    
    
    
    
                if fdate != "" {
                    elements.setObject(fdate, forKey: "Date_Text")
                }
    
                feeds.addObject(elements)
            }
    
        }
        func parser(parser: NSXMLParser, foundCharacters string: String?) {
    
            if element.isEqualToString("DosNum") {
                ftitle.appendString(string!)
    
            }else if element.isEqualToString("Date_Text") {
                fdate.appendString(string!)
    
            }else if element.isEqualToString("Dossier_number"){
                fcategory.appendString(string!)
            }
    
        }
    

    这是我的第一个TableView代码。

    var myFeed : NSArray = []
    var url: NSURL = NSURL()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Cell height.
        self.tableView.rowHeight = 70
        self.tableView.dataSource = self
        self.tableView.delegate = self
    
        //url = NSURL(string: "https://www.kpmg.com/_layouts/feed.aspx?xsl=1&web=/RU/ru/IssuesAndInsights/RSSFeeds&page=207b36b2-20f7-407f-a9ec-a09f191fd84b&wp=9810a349-6086-489d-ad03-40c06f6669f6")!
        //url = NSURL(string: "http://www.skysports.com/rss/0,20514,11661,00.xml")!
        // url = NSURL(scheme: "https", host: "www.kpmg.com", path: "/_layouts/feed.aspx?xsl=1&web=/RU/ru/IssuesAndInsights/RSSFeeds&page=207b36b2-20f7-407f-a9ec-a09f191fd84b&wp=9810a349-6086-489d-ad03-40c06f6669f6")!
        //(scheme: "http", host: "10.207.203.216", path: "/AppWebservice/Service1.asmx/getNewsData")!
        url = NSURL(string: "http://10.207.206.74/AppWebservice/Service1.asmx/getUniqDossier")!
        // Call custom function.
        loadRss(url);
    
    }
    
    override func viewWillAppear(animated: Bool) {
    
    
        super.viewWillAppear(animated)
    
        self.navigationItem.title = ""
    
    
        let backImg:UIImage! = UIImage(named: "backPicture.png")
    
    
        self.navigationItem.backBarButtonItem =
            UIBarButtonItem(image:backImg, style:.Plain, target:self, action:nil);
    
        let navBgImage:UIImage = UIImage(named: "express.png")!
        self.navigationController?.navigationBar.setBackgroundImage(navBgImage, forBarMetrics: .Default)
    }
    
    
    func loadRss(data: NSURL) {
    
    
        let myParser = DossierParser(URL: data)
    
        myFeed = myParser.feeds
    
        tableView.reloadData()
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
    
    
        if segue.identifier == "openPage" {
    
            let indexPath: NSIndexPath = self.tableView.indexPathForSelectedRow!
            let selectedFTitle: String = myFeed[indexPath.row].objectForKey("DosNum") as! String
            let selectedFContent: String = myFeed[indexPath.row].objectForKey("Dossier_number") as! String
            // Instance of our feedpageviewcontrolelr
            let fpvc: FeedPageViewController = segue.destinationViewController as! FeedPageViewController
            fpvc.selectedFeedTitle = selectedFTitle
            fpvc.selectedFeedFeedContent = selectedFContent
        }
    }
    
    
    // MARK: - Table view data source
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myFeed.count
    }
    
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    
        let chevron = UIImage(named: "Next4.png")
        cell.accessoryType = .DisclosureIndicator
        cell.accessoryView = UIImageView(image: chevron!)
        cell.textLabel?.textColor = UIColor.blackColor()
        // Feeds dictionary.
        var dict : NSDictionary! = myFeed.objectAtIndex(indexPath.row) as! NSDictionary
    
        // Set cell properties.
        cell.textLabel?.text = myFeed.objectAtIndex(indexPath.row).objectForKey("DosNum") as? String
    
    
    
    
    
    
    
        cell.detailTextLabel?.text = myFeed.objectAtIndex(indexPath.row).objectForKey("Date_text") as? String
    
        return cell
    
    }
    

    我希望得到任何帮助。我猜测应该有更简单的方法,但我不知道,所以如果有人有建议那就太好了!

    谢谢!

1 个答案:

答案 0 :(得分:1)

我相信您希望保留fCategory的实例,并通过init将其传递给第二个解析器类。

第二个解析器类应具有以下内容,以允许传入fcategory并存储在本地以供解析器使用。

let fcategory = NSMutableString()
let URL = NSURL

init(URL: NSURL, category: String){
    super.init()

    self.URL = URL
    self.fcategory = category

    self.startParse(URL, fcategory)
}