简体   繁体   English

表格视图单元格未显示相应网站

[英]Table View Cells Not Showing Corresponding Website

I created a web view to display data on a table view controller;我创建了一个 Web 视图来在表视图控制器上显示数据; however, The table view cells do not show the corresponding URL links retrieved from newsapi.com.但是,表格视图单元格不显示从 newsapi.com 检索到的相应 URL 链接。 How do I resolve this issue to make it show the right website when the cell is selected?如何解决此问题以使其在选择单元格时显示正确的网站? Project link: https://github.com/lexypaul13/Covid-News项目链接: 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.
    }
}

you are not consistent between the 3 methods used for the tableView.您在用于 tableView 的 3 种方法之间不一致。

  • In numberOfRowsInSection you use filteredArticles and news.articlesnumberOfRowsInSection 中,您使用filteredArticlesnews.articles
  • In cellForRowAt you only use news.articlescellForRowAt你只使用news.articles
  • andIn didSelectRowAt you use newsSelected which set as a lazy var so only set once and never update.并且在didSelectRowAt 中,你使用newsSelected ,它设置为一个惰性变量,所以只设置一次并且永远不会更新。

Better use filteredArticles and news.articles in the same way for both 3 methods更好地使用filteredArticlesnews.articles以同样的方式用于3种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM