简体   繁体   中英

Passing data from TableViewController to parser

During my app development I've encountered a specific problem. I've a database which contains following columns: Dossier_Title, Dossier_Name, Dossier_Category and Dossier_Description. The Description is uniqe for every Dossier.

  1. First of all I'm calling my WebService which selects unique Dossier_Title and forms an xml page. After that app parse this page and forms a number of unique cells in table View. Also it forms an array which consists of Dossier_Title and Dossier_category.
  2. Now I want to form a new tableView which would consist of Dossier_Name and Dossier_Description based on the Dossier_Category I've achieved on previous step. For this I want to call a new WebService and parse it using that Category as condition.

My question is: how can I pass Dossier_category to my second parser so I can use it as condition? Here is my first parser code I assume the second one would be pretty much the same with addition of some new conditions

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!)
        }

    }

And here is my first TableView code.

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

}

I would appreciate any sort of help. My guessing there should be easier way around it, but I don't know it, so if someone has suggestion it would be nice!

Thanks!

I believe you would want to retain an instance of fCategory and pass it to the second parser class via init.

The second parser class should have the following to allow for the fcategory to be passed in and stored locally for use by the parser.

let fcategory = NSMutableString()
let URL = NSURL

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

    self.URL = URL
    self.fcategory = category

    self.startParse(URL, fcategory)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM