【发布时间】:2015-09-14 10:26:41
【问题描述】:
我需要创建一个 tableview 并用我使用 json 获取的数据库信息填充它。这是我使用 json 从数据库中得到的响应
{
"news": [
{
"id": "35",
"type": "news",
"title": "final test for offer",
"city": "Mumbai",
"description": "Test description",
"image": "http://www.saimobileapp.com/mobileappbackend/news/IMG_0421.JPG"
},
{
"id": "31",
"type": "news",
"title": "new test",
"city": "Mumbai",
"description": "yes its a test msg",
"image": "http://www.saimobileapp.com/mobileappbackend/news/Chrysanthemum.jpg"
},
{
"id": "30",
"type": "news",
"title": "This is a test news",
"city": "Mumbai",
"description": "Test description",
"image": "http://www.saimobileapp.com/mobileappbackend/news/1.jpg"
}
]
}
这是 3 个不同的新闻,标题等,所以我需要计算它,因为我将添加新的,并在此基础上创建一个表格视图。
这是我现在使用新 EDIT 获取数据库信息的代码:
func LoadNews() {
let post:NSString = ""
NSLog("PostData: %@",post);
let url:NSURL = NSURL(string: "http://saimobileapp.com/services/sai_news.php")!
let postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!
let postLength:NSString = String( postData.length )
let request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var reponseError: NSError?
var response: NSURLResponse?
var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&reponseError)
if ( urlData != nil ) {
let res = response as! NSHTTPURLResponse!;
NSLog("Response code: %ld", res.statusCode);
if (res.statusCode >= 200 && res.statusCode < 300)
{
let responseData:NSString = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!
NSLog("Response ==> %@", responseData);
var error: NSError?
var Title: [String] = []
if let jsonData = NSJSONSerialization.JSONObjectWithData(urlData!, options: nil, error: &error) as? [String:AnyObject] { // dictionary
if let locationsArray = jsonData["news"] as? [[String:AnyObject]] { // array of dictionaries
for locationDictionary in locationsArray { // we loop in the array of dictionaries
if let location = locationDictionary["title"] as? String { // finally, access the dictionary like you were trying to do
Title.append(location)
var SaveTitle = save.setObject(Title, forKey: "NewsTitle")
}
}
}
}
}
}
}
对于 TableView,我现在使用它:
// MARK: UITextFieldDelegate Methods
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var FormName = save.arrayForKey("NewsTitle")!
return FormName.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var FormName = save.arrayForKey("NewsTitle")!
var cell:UITableViewCell = self.TableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell
let row = indexPath.row
cell.textLabel?.text = FormName[indexPath.row] as! String
if (indexPath.row % 2 == 0) {
cell.backgroundColor = UIColor.clearColor()
}else{
cell.backgroundColor = UIColor.clearColor()
cell.textLabel?.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.0)
}
cell.textLabel?.textColor = UIColor.whiteColor()
return cell
}
// MARK: UITableViewDelegate Methods
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
TableView.deselectRowAtIndexPath(indexPath, animated: false)
let row = indexPath.row
点击单元格时如何在第二页显示说明?
谁能帮帮我?提前致谢。
【问题讨论】: