我正在为iPhone构建一个非常基本的reddit客户端,只是为了练习iOS开发。我已经得到它来解析我选择的subreddit的JSON,但是我无法从解析的JSON中获取标题并将它们显示在我的UITableView中。我现在遇到的问题是,在加载UITableView之前,我无法使用我的“postList”数组填充JSON数据。因此,当正在填充表视图单元格时,postList数组的元素中没有任何内容可供填充。知道如何解决这个问题吗?
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let URL = "https://www.reddit.com/r/swift/.json"
var postList : [PostList] = Array(repeating: PostList(), count: 26)
//var postList : [PostList] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView.dataSource = self
tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Setting up tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "aroundMeCell", for: indexPath)
getPosts(url: URL, row: indexPath.row, cell: cell)
let title = postList[indexPath.row].title
cell.textLabel?.text = title
return cell
}
//Receiving posts using Alamofire
func getPosts(url: String, row: Int, cell: UITableViewCell) {
Alamofire.request(url, method: .get)
.responseJSON { response in
if response.result.isSuccess {
let postJSON : JSON = JSON(response.result.value!)
//print(postJSON)
self.createPostListArray(json: postJSON, row: row, cell: cell)
} else {
print("Error: \(String(describing: response.result.error)) aaaaaa")
}
}
}
func createPostListArray(json: JSON, row: Int, cell: UITableViewCell) {
let titlePath : [JSONSubscriptType] = ["data", "children", row, "data", "title"]
let postPath : [JSONSubscriptType] = ["data", "children", row, "data", "selftext"]
//Making sure I can unwrap both
if(json[titlePath].string != nil && json[postPath].string != nil){
let inTitle = json[titlePath].string!
let inPost = json[postPath].string!
postList[row].title = inTitle
postList[row].post = inPost
}
else{
print("error")
}
}
答案 0 :(得分:1)
您的代码存在多个问题。
1- var postList : [PostList] = Array(repeating: PostList(), count: 26)
。您可以创建空数组var postList = [PostList]()
并在响应时追加删除元素。
2-在cellForRowAt
上呼叫api并不是一个好主意。 原因:每当调用此方法时,都会调用api。考虑一下这个场景,即你要为每个叫做api的队列滚动你的桌面视图。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let URL = "https://www.reddit.com/r/swift/.json"
var postList = [PostList]()
override func viewDidLoad() {
super.viewDidLoad()
getPosts(url: URL)
tableView.dataSource = self
tableView.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Setting up tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "aroundMeCell", for: indexPath)
let title = postList[indexPath.row].title
cell.textLabel?.text = title
return cell
}
//Receiving posts using Alamofire
func getPosts(url: String) {
Alamofire.request(url, method: .get)
.responseJSON { [weak self] response in
if response.result.isSuccess {
let postJSON : JSON = JSON(response.result.value!)
//print(postJSON)
self?.createPostListArray(json: postJSON)
self?.tableView.reloadData()
} else {
print("Error: \(String(describing: response.result.error)) aaaaaa")
}
}
}
func createPostListArray(json: JSON) {
//parse your json here, or read about objectMapper a pod. This will help you in parsing and if you are using swift 4, read encode json.
}
答案 1 :(得分:0)
首先在viewDidLoad()中执行api调用。将数组声明为
var postList = [PostList]
在api调用成功块之后执行self.tableview.reloadData()。