簡體   English   中英

表格視圖單元格未顯示相應網站

[英]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 種方法之間不一致。

  • numberOfRowsInSection 中,您使用filteredArticlesnews.articles
  • cellForRowAt你只使用news.articles
  • 並且在didSelectRowAt 中,你使用newsSelected ,它設置為一個惰性變量,所以只設置一次並且永遠不會更新。

更好地使用filteredArticlesnews.articles以同樣的方式用於3種方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM