2015-09-24 55 views
0

在我的應用程序開發過程中,我遇到了一個特定的問題。 我有一個數據庫,其中包含以下列:Dossier_Title,Dossier_Name,Dossier_Category和Dossier_Description。說明對每個檔案都是唯一的。將數據從TableViewController傳遞到解析器

  1. 首先我打電話給我的WebService,它選擇唯一的Dossier_Title並形成一個XML頁面。之後,應用程序解析此頁面並在表格視圖中形成許多獨特的單元格。它也形成一個由Dossier_Title和Dossier_category組成的數組。
  2. 現在我想要形成一個新的tableView,它由Dossier_Name和Dossier_Description組成,這個Dossier_Description是基於我在上一步中實現的Dossier_Category。爲此,我想調用一個新的WebService並使用該Category作爲條件來解析它。

我的問題是:如何將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

我相信你會想保留一個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) 
} 
+0

是的,我想出瞭如何通過類別,但仍然,感謝您的努力! –

相關問題