Ios Swift uitableview从API加载更多内容
我从APi中得到20个字符 在tableview中,但此处493个字符和一个页面仅显示20个字符。如何在tableview中显示493个字符? 有人能帮我吗? 我正在寻找有关谷歌的信息,但找不到我问题的答案 此处代码:Ios Swift uitableview从API加载更多内容,ios,swift,Ios,Swift,我从APi中得到20个字符 在tableview中,但此处493个字符和一个页面仅显示20个字符。如何在tableview中显示493个字符? 有人能帮我吗? 我正在寻找有关谷歌的信息,但找不到我问题的答案 此处代码: class UsersTableViewController: UITableViewController { var characters = [Results]() override func viewDidLoad() { s
class UsersTableViewController: UITableViewController {
var characters = [Results]()
override func viewDidLoad() {
super.viewDidLoad()
LoadCharacters()
}
func LoadCharacters() {
let urlString = "https://rickandmortyapi.com/api/character/"
if let url = URL(string: urlString)
{
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url) { (data, responce, error) in
if error != nil {
print(error!)
return
}
if let safeData = data {
self.parseJson(usersData: safeData)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
task.resume()
self.tableView.reloadData()
}
}
func parseJson(usersData: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(JSONData.self, from: usersData)
characters = decodedData.results
print(decodedData.results[0].name)
} catch {
print(error)
}
}
struct JSONData: Decodable {
let results: [Results]
}
struct Results: Decodable {
let name: String
}
//MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return characters.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "user", for: indexPath)
let guys = characters[indexPath.row]
cell.textLabel?.text = guys.name
return cell
}
}
首先,您需要创建一个与API调用响应匹配的结构 您可以从中轻松创建结构。 然后您可以直接将响应转换为字符数组 下一步是使用数组显示数据 将tableview项计数设置为结果数组计数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return charcters.count
}
在单元格中显示字符数据。不要在单元格标记中添加字符id。因为用户单击行时需要它
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! CharcterCell
let item = characters[indexPath.row]
cell.lblName.text = item.characterName
cell.tag = item.chacterId
return cell
}
Hi@Kyrylovep如果查找此URL JSON的第一行,您将看到以下内容{info:{count:493,pages:25,下一步:https://rickandmortyapi.com/api/character/?page=2,上:}。它们已经有了下一个分页和预览。您需要在代码上实现该请求,以便将更多结果附加到tableview@DanielArantesLoverde嗨,这就是我不知道如何在代码中实现的问题,所以我在这里寻求帮助。也许你有一个如何在代码中实现的例子?好的,有这里有一堆代码在教你这个,请寻找带分页或延迟加载的uitableview,理解代码,然后适应你的代码。不是那么简单,但我相信你能做到!