问题是----> TableView在所有单元格中显示相同的标题和分布
我的项目ViewController:
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var articles: [Article]? = []
override func viewDidLoad() {
super.viewDidLoad()
fetchArticles()
}
func fetchArticles(){
let urlRequest = URLRequest(url: URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=horses&type=video&maxResults=10&key=(apiKey)")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error as Any)
return
}
self.articles = [Article]()
do {
let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String : Any]
let article = Article()
if let articlesFromJson = json?["items"] as? [[String : Any]] {
for item in articlesFromJson {
if let snippet = item["snippet"] as? [String : Any],let title = snippet["title"]as? String,let desc = snippet["description"]as? String {
article.headline = title
article.desc = desc
self.articles?.append(article)
}
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
}
task.resume()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell", for: indexPath) as? ArticleCell
cell?.title.text = self.articles?[indexPath.row].headline!
cell?.desc.text = self.articles?[indexPath.row].desc!
return cell!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.articles?.count ?? 0
}
}
Article.Swift:
import UIKit
class Article: NSObject {
var headline: String?
var desc: String?
}
**ArticleCell :**
import UIKit
class ArticleCell: UITableViewCell {
@IBOutlet weak var title: UILabel!
@IBOutlet weak var desc: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
问题是----> TableView在所有单元格中显示相同的标题和分布
答案 0 :(得分:0)
只需评论此行
DispatchQueue.main.async {
self.tableview.reloadData()
}
/ 并在for循环后添加它
在for循环中插入let article = Article()
/
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
var articles: [Article]? = []
override func viewDidLoad() {
super.viewDidLoad()
fetchArticles()
}
func fetchArticles(){
let urlRequest = URLRequest(url: URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&q=horses&type=video&maxResults=10&key=(apiKey)")!)
let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
if error != nil {
print(error as Any)
return
}
self.articles = [Article]()
do {
let json = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String : Any]
if let articlesFromJson = json?["items"] as? [[String : Any]] {
for item in articlesFromJson {
if let snippet = item["snippet"] as? [String : Any],let title = snippet["title"]as? String,let desc = snippet["description"]as? String {
let article = Article()
article.headline = title
article.desc = desc
self.articles?.append(article)
}
}
self.tableview.reloadData()
}
/*DispatchQueue.main.async {
self.tableview.reloadData()
} */
}
}
task.resume()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "articleCell", for: indexPath) as? ArticleCell
cell?.title.text = self.articles?[indexPath.row].headline!
cell?.desc.text = self.articles?[indexPath.row].desc!
return cell!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.articles?.count ?? 0
}
}