解析XML结果后我得到了这个结果。
当我做println()
时,我得到了完全像这样的结果
{
description = "Suzuki SX4 - BB71521";
deviceID = 359710042040320;
}
{
description = "Chevrolet Tahoe Noir - Demonstration";
deviceID = 359710042067463;
}
{
description = "Isuzu D'Max AA-08612";
deviceID = 359710042091273;
}
{
description = "Toyota 4 Runner";
deviceID = 359710042110768;
}
但是当我尝试使用两个单元格在UITableView
中解析相同的结果时,UITableView
显示为空。
请帮我填一下表???
这是我的代码
import UIKit
class mtTableViewController: UITableViewController, NSXMLParserDelegate {
var info = NSDictionary();
var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
override func viewDidLoad()
{
super.viewDidLoad()
self.beginParsing()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func beginParsing()
{
let url = NSURL(string: "http://localhost:8080")
var auth =
"<GTSRequest command=\"dbget\">" +
"<Authorization account=\"" + "easytrucking" + "\" user=\"" + "admin" + "\" password=\"" + "T8095634934ht" + "\"/>" +
"<Record table=\"Device\" partial=\"true\">" +
"<Field name=\"accountID\">" + "easytrucking" + "</Field>" +
"<Field name=\"description\"/>" +
"</Record>" +
"</GTSRequest>";
// "<Authorization account=\"" + txtAccount.text + "\" user=\"" + txtUserName.text + "\" password=\"" + txtPassword.text + "\"/>" +
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let data : NSData = (auth).dataUsingEncoding(NSUTF8StringEncoding)!;
request.HTTPBody = data;
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
/// println(NSString(data: data, encoding: NSUTF8StringEncoding))
let xml = SWXMLHash.parse(data)
let count = xml["GTSResponse"]["Record"].all.count
for var i = 0; i < count; i++
{
if (xml["GTSResponse"]["Record"][i]["Field"][1].element?.attributes["name"] == "deviceID")
{
self.elements.setObject((xml["GTSResponse"]["Record"][i]["Field"][1].element?.text)!, forKey: "deviceID")
}
if (xml["GTSResponse"]["Record"][i]["Field"][3].element?.attributes["name"] == "description")
{
self.elements.setObject((xml["GTSResponse"]["Record"][i]["Field"][3].element?.text)!, forKey: "description")
}
self.posts.addObject(self.elements)
// here i print the result
println(self.elements)
}
}
}
//Tableview Methods
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return posts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var cell : UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
if(cell == nil) {
cell = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil)[0] as UITableViewCell;
}
cell.textLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("deviceID") as NSString
cell.detailTextLabel?.text = posts.objectAtIndex(indexPath.row).valueForKey("description") as NSString
return cell as UITableViewCell
}
}
请帮助!!!
答案 0 :(得分:0)
有两个问题。首先,NSURLConnection.sendAsynchronousRequest()
- 如名称所示 - 异步工作:它启动网络
请求然后返回。稍后将调用完成处理程序
数据已经到了。然后你必须用
tableView.reloadData()
用新数据填充数据源数组后。
第二,在
for var i = 0; i < count; i++
{
self.elements.setObject(..., forKey: ...)
self.elements.setObject(..., forKey: ...)
// ...
self.posts.addObject(self.elements)
}
您一次又一次地修改相同的字典。 NSMutableArray
并且NSMutableDictionary
是引用类型,因此您最终会成功
包含指向同一字典的count
指针的数组
(包含上一次循环执行的数据)。
而不是将单个字典作为属性
var elements = NSMutableDictionary()
这应该是循环中的局部变量:
for var i = 0; i < count; i++
{
var elements = NSMutableDictionary()
self.elements.setObject(..., forKey: ...)
self.elements.setObject(..., forKey: ...)
// ...
self.posts.addObject(elements)
}
您也可以考虑使用Swift数组和字典。 这些是值类型,这使得这种错误更少 可能的。