[英]Table View Cells Not Showing Corresponding Website
我創建了一個 Web 視圖來在表視圖控制器上顯示數據; 但是,表格視圖單元格不顯示從 newsapi.com 檢索到的相應 URL 鏈接。 如何解決此問題以使其在選擇單元格時顯示正確的網站? 項目鏈接: https : //github.com/lexypaul13/Covid-News
extension LatestNewsViewController: UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering{
return filteredArticles?.count ?? 0
}
return news.articles?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as! NewsTableViewCell
var articleToUse = news.articles
if isFiltering{
articleToUse = news.articles
}
cell.authorName.text = articleToUse?[indexPath.row].author
cell.headLine.text = articleToUse?[indexPath.row].myDescription
// cell.newsImage.downloadImage(url:(row?.urlImage ?? "nill"))
if let dateString = articleToUse?[indexPath.row].publishedAt,
let date = indDateFormatter.date(from: dateString){
let formattedString = outDateFormtter.string(from: date)
cell.timePublication.text = formattedString
} else {
cell.timePublication.text = "----------"
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.urlSelected = newsSelected?[indexPath.row].urlWebsite ?? ""
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "article"{
if table_view.indexPathForSelectedRow != nil{
let destinationController = segue.destination as! ArticleViewController
destinationController.url = self.urlSelected
}
}
}
class ArticleViewController: UIViewController {
@IBOutlet weak var articlePage: WKWebView!
var url : String = "http://newsapi.org/v2/everything?q=coronavirus&sortBy=popularity&apiKey=d32071cd286c4f6b9c689527fc195b03&pageSize=50&page=2"
override func viewDidLoad() {
super.viewDidLoad()
if let url = URL(string: url ) {
let request = URLRequest(url: url)
articlePage.load(request)
// Do any additional setup after loading the view.
}
}
您在用於 tableView 的 3 種方法之間不一致。
更好地使用filteredArticles和news.articles以同樣的方式用於3種方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.