我从statePicker中选择一个状态,然后将状态传递给URL,然后解析XML并将XML上传到TableView Cell。但是,当我选择一个新状态时,它会解析XML并将数据添加到TableView
单元格,但之前选择的状态数据不会从TableViewCell
class ViewController: UIViewController{
@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var statePicker: UIPickerView!
let statePicked:[String] = ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "PH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "WA", "WV", "WI", "WY"]
var weatherEvent:[String] = []
var weatherExpires:[String] = []
var weatherSummary:[String] = []
var weatherEffective:[String] = []
var weatherUrgency:[String] = []
var weatherSeverity:[String] = []
var weatherCertainty:[String] = []
var weatherLink:[String] = []
var weatherPolygon:[String] = []
var xmlParser: NSXMLParser!
var url: NSURL!
var concate: String = ""
var entryFound:Bool = false
var eventFound: Bool = false
var expiresFound: Bool = false
var summaryFound:Bool = false
var effectiveFound:Bool = false
var urgencyFound:Bool = false
var severityFound:Bool = false
var certaintyFound:Bool = false
var linkFound:Bool = false
var polygonFound:Bool = false
var link: String = ""
var state: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
statePicker.delegate = self
statePicker.dataSource = self
xmlParser?.delegate = self
myTableView.delegate = self
myTableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "ShowDetail"){
let detailVC = segue.destinationViewController as! DetailViewController
if let indexPath = myTableView.indexPathForCell(sender as! WeatherCell){
detailVC.event = weatherEvent[indexPath.row]
detailVC.expires = weatherExpires[indexPath.row]
detailVC.certainty = weatherCertainty[indexPath.row]
detailVC.summary = weatherSummary[indexPath.row]
detailVC.expires = weatherExpires[indexPath.row]
detailVC.effective = weatherEffective[indexPath.row]
detailVC.urgency = weatherUrgency[indexPath.row]
detailVC.severity = weatherSeverity[indexPath.row]
detailVC.link = weatherLink[indexPath.row]
}
}
}
}
扩展码:
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return self.statePicked.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return self.statePicked[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
state = self.statePicked[row]
concate = "http://alerts.weather.gov/cap/".stringByAppendingString(state.lowercaseString) + ".php?x=0"
url = NSURL(string: concate)
xmlParser = NSXMLParser(contentsOfURL: url!)
xmlParser?.delegate = self
xmlParser?.parse()
myTableView.reloadData()
print("state after \(state.lowercaseString)")
}
}
任何帮助?