我正在尝试从我的网站检索数据,我正在使用Alamofire,我怎样才能将这些数据放入一个可用于填充表格视图的数组中?
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request("http://lytestech.ga/api/lytes/get_movies/").responseJSON { response in
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
let res = json as! NSDictionary
let movies = res["movies"] as! NSArray
// movieTitles = movies["movie_desc"]
let movieTitles: [String] = movies["movietitile"] as! String
print (movies)
print (movieTitles)
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
}
json数据
JSON: {
movies = (
{
id = 66;
"movie_desc" = "spiders bite";
movietitile = spiderman;
},
{
id = 64;
"movie_desc" = horror;
movietitile = mummy;
}
);
status = ok;
}
(
{
id = 66;
"movie_desc" = "spiders bite";
movietitile = spiderman;
},
{
id = 64;
"movie_desc" = horror;
movietitile = mummy;
}
)
答案 0 :(得分:0)
将您的回复Data
转换为Dictionary
let dictionary: Dictionary? = NSKeyedUnarchiver.unarchiveObject(with: response.data) as! [String : Any]
从Array
中提取您的Dictionary
,密钥名称为movies
if let arryMovies1 = dictionary["movies"] as? [[String:Any]] {
print (arryMovies1);
// Now your have your Array
// You can Populate into UItableView
// when your array is modified than you have to reload the tableData
self.arryMovies=arryMovies1
self.tableView.reloadData()
}
在cellForRowAtIndexPath
填充列表视图
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "you_cell_identifre") as! UITableCell
let dicMovie = self.arryMovies[indexPath.row] as! NSDictionary
cell.lableTitle.text = dicMovie.value(forKey: "movietitile") as? String
return cell
}
我希望这会对你有所帮助
答案 1 :(得分:0)
var movies:[Dictionary<String,AnyObject>] = [] // Makes movies array global
movies = res["movies"] // and tableview reload
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: identifier) ??
UITableViewCell(style: .default, reuseIdentifier: identifier)
cell.textLabel!.text = movies[indexPath.row]["movietitile"] as? String
return cell
}
答案 2 :(得分:0)
将电影存储在电影NSArray
var movies = NSArray()
在cellForRowAt
方法中,您可以显示这样的数据。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier") as! YouCustomCell
let movieDetail = movies[indexPath.row] as! NSDictionary
cell.lblMovieName.text = movieDetail.value(forKey: "movietitile") as? String
return cell
}
注意: - 这只是代码框架,您可以根据自定义单元格和Outlets使用
答案 3 :(得分:0)
func demo()
{
let str : String = "http://lytestech.ga/api/lytes/get_movies/"
let url = NSURL(string:str)
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request)
.responseString { response in
switch (response.result) {
case .Success(let JSON):
let data = JSON.dataUsingEncoding(NSUTF8StringEncoding)!
do {
let responseString = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
// print(responseString!)
self.movieName = []
self.desc = []
let arr = responseString!["movies"] as! NSArray
for j in 0..<arr.count{
let dic = arr[j] as! NSMutableDictionary
self.movieName.addObject(dic.valueForKey("movietitile")!)
self.desc.addObject(dic.valueForKey("movie_desc")!)
}
self.removeIndicator()
self.contactTable.reloadData()
} catch _ as NSError {
//print(error)
}
break
case .Failure:
print("FAILURE")
self.removeIndicator()
break
}
}
}