在 swift 中填充表格视图
Populate tableview in swift
我在解析 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
显示为空。
请帮我只填写 table ???
这是我的代码
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
}
}
请帮忙!!!
有两个问题。首先,NSURLConnection.sendAsynchronousRequest()
– 顾名思义 – 异步工作:它启动一个网络
请求然后 returns。稍后调用完成处理程序,当
数据已经到达。然后你必须用
重新加载 table 视图
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 数组和字典。
这些是 值类型 这使得这种错误更少
有可能。
我在解析 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
显示为空。
请帮我只填写 table ???
这是我的代码
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
}
}
请帮忙!!!
有两个问题。首先,NSURLConnection.sendAsynchronousRequest()
– 顾名思义 – 异步工作:它启动一个网络
请求然后 returns。稍后调用完成处理程序,当
数据已经到达。然后你必须用
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 数组和字典。 这些是 值类型 这使得这种错误更少 有可能。