我正在为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")
}
}
最佳答案
您的代码有多个问题。
1-var postList : [PostList] = Array(repeating: PostList(), count: 26)
。您可以创建空数组var postList = [PostList]()
并在响应时追加delete元素。
2-在cellForRowAt
上调用api根本不是一个好主意。原因:每当这个方法被调用时,你就调用一个api。考虑这样一个场景,即您正在为将调用此api的每个出列滚动tableview。
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.
}