我是一個swift和UITableView的新手,我從請求中獲取數據,並試圖在表視圖上顯示結果。 我得到的數據非常好,但我的表格視圖不滾動。我知道有這個問題的許多問題,但我看到別人的答案,我解決不了我的存在啞代碼:UiTableView cellForRowAt不返回良好的價值,不能滾動
@IBOutlet weak var tav: UITableView!
//Emoji array dynamically created
var emojisArray: [Emoji] = []
{
didSet {
tav.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
tav.dataSource = self;
tav.delegate = self;
//backgroundColor
self.view.backgroundColor = UIColor(red: 187, green: 222/255, blue: 251, alpha: 1)
//Url request
let url = "http://localhost:8888/emoji-api/web/app_dev.php/emojis"
let request = NSMutableURLRequest(url: URL(string: url)!)
request.httpMethod = "GET"
let requestAPI = URLSession.shared.dataTask(with: request as URLRequest) {data, response, error in
if (error != nil) {
print(error!.localizedDescription) // On indique dans la console ou est le problème dans la requête
}
if let httpStatus = response as? HTTPURLResponse , httpStatus.statusCode != 200 {
print("statusCode devrait être de 200, mais il est de \(httpStatus.statusCode)")
print("réponse = \(String(describing: response))") // On affiche dans la console si le serveur ne nous renvoit pas un code de 200 qui est le code normal
}
// let responseAPI = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
//print("responseString = \(String(describing: responseAPI))") // Affiche dans la console la réponse de l'API
if error == nil {
// Ce que vous voulez faire.
DispatchQueue.main.async {
do {
// let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)
if let parsedData = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]]{
for item in parsedData {
let emoji = Emoji()
//Transform unicode on an Emoji
let strUnicodeEmoji = String(UnicodeScalar(Int(item["unicode"] as! String, radix: 16)!)!)
print(strUnicodeEmoji)
emoji.emojiString = strUnicodeEmoji as String;
emoji.description = item["description"] as! String;
self.emojisArray.append(emoji)
}
}
}
catch {
print("Could not serialise")
}
}
}
}
requestAPI.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return emojisArray.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell();
print(emojisArray.count)
let emoji = emojisArray[indexPath.row];
cell.textLabel?.text = emoji.emojiString;
return cell;
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let defVC = segue.destination as!
SecondViewController;
defVC.emojiSelect = sender as! Emoji
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let emojiSelect = emojisArray[indexPath.row];
performSegue(withIdentifier: "secondScreen", sender: emojiSelect)
}
我不修改滾動視圖默認值。 謝謝
電話'tableView.reloadData註冊您的()'在你的完成塊中添加'self.emojisArray'後你的網絡調用中 – NSGangster
這是Swift。擺脫所有這些分號。並請使用表格視圖進行一些搜索。你在'cellForRowAt'中創建單元格的代碼都是錯誤的。搜索'dequeueReusableCell' – rmaddy
這是來自其他語言的反射 –