微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在表格视图单元格中实现分页

如何解决如何在表格视图单元格中实现分页

我的应用程序从newsAPI.com检索json。当我查看从Web服务返回的json时,它显示了2000个值,但是它返回了加载到我的tableview控制器中的20个值。如何增加该值,以便在用户向下滚动表视图时向他们显示更多值,这些值已加载到表视图控制器中?

class LatestNewsViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

let newsData = Articles() //Model object

let urlRequest = "https://newsapi.org/v2/everything?q=coronavirus&apiKey=d32071cd286c4f6b9c689527fc195b03" //Website API
var urlSelected = ""
var articles: [Articles]? = [] // holds array of Articles data
var indexOfPagetoRequest = 1
@IBOutlet weak var table_view: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    table_view.cellLayoutMarginsFollowReadableWidth = true
    navigationController?.navigationBar.prefersLargeTitles = true
    retriveData( )
    
    
}

func retriveData(  )->Int{
    guard let aritcleUrl = URL(string: urlRequest) else { //send a request to the server
        return n
    }
    
    let request = URLRequest(url: aritcleUrl)
    let task = URLSession.shared.dataTask(with: request,completionHandler: { (data,response,error) -> Void in //collects content from website
        
        if  error != nil { // checks if content is available
            print(error ?? 0)
            return
        }
        if let data = data { // converts data to an array of Article objects
            self.articles = self.parseData(data: data)
            
            
            
        }
        
        
    })
    
    task.resume()
    return n
}


func parseData(data:Data)-> [Articles]   {
    var articles: [Articles]? = [] // holds parsed data
    
    do {
        let jsonResult = try JSONSerialization.jsonObject(with: data,options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
        
        let jsonArticles = jsonResult?["articles"] as? [AnyObject] ?? [] // gets first head of json file and converts it to dictionary
        
        for jsonArticle in jsonArticles{ // captures data and stores it in the model object
            let article = Articles()
            article.author = jsonArticle["author"] as? String
            article.title = jsonArticle["description"] as? String
            article.publishedAt = jsonArticle["publishedAt"] as? String
            article.urlImage = jsonArticle["urlToImage"] as? String
            article.urlWebsite = jsonArticle["url"] as? String
            articles?.append(article) //put article data in the array
        }
        
        print(jsonArticles)
        
        dispatchQueue.main.async {
            
            if(articles!.count > 0)
            {
                self.table_view.reloadData()
            }
        }
        
    } catch {
        print("nothing my guy\(error)")
    }
    
    return articles ??  [] // returns an array of articles
}

 



func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
    return articles?.count ?? 0
}


func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell1",for: indexPath) as! NewsTableViewCell
    cell.authorName.text = articles?[indexPath.row].author
    cell.headLine.text = articles?[indexPath.row].title
    cell.newsImage.downloadImage(from:(self.articles?[indexPath.item].urlImage ?? "nill"))
    cell.timePublication.text = articles?[indexPath.row].publishedAt

    
    

    return cell
}


  [1]: https://i.stack.imgur.com/OY5G5.png

解决方法

首先,我将检查表视图的约束,因为这是一个常见问题(通常为0,0)

此外,我还要在属性检查器中检查“已启用滚动”和“重用标识符”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。