gpt4 book ai didi

iOS:tableView.reloadData() 在 swift3 中不起作用

转载 作者:行者123 更新时间:2023-11-28 12:31:16 25 4
gpt4 key购买 nike

我是 Swift 的新手,我试图在 Swift 中更新数据后重新加载我的 TableView ,但它似乎不起作用。当我更改选项卡并返回时,tableView 会重新加载但不会自动加载。这是我的代码:

我的 TableView Controller :

 class AllChannelTableViewController: UITableViewController {

var modelChannel = [AllCnannelModel]()

let realm = try! Realm()

lazy var channels: Results<ChannelData> = { self.realm.objects(ChannelData.self) }()

override func viewDidLoad() {
super.viewDidLoad()
defaultChannel()
}

func defaultChannel() {

if channels.count == 0 {

SVProgressHUD.show(withStatus: "dowload")

let URL = "http://52.50.138.211:8080/ChanelAPI/chanels"

Alamofire.request(URL).responseArray { (response: DataResponse<[AllCnannelModel]>) in

let channelArray = response.result.value

if let channelArray = channelArray {
for channel in channelArray {
try! self.realm.write() {
let newChannel = ChannelData()
newChannel.category_id = channel.category_id!
newChannel.idChannel = channel.id!
print(newChannel.idChannel)
newChannel.name = channel.name!
print(newChannel.name)
newChannel.picture = channel.picture
newChannel.url = channel.url!
print(newChannel.url)
self.realm.add(newChannel, update: true)
}
}
}
}
DispatchQueue.main.async{
self.tableView.reloadData()
self.channels = self.realm.objects(ChannelData.self)
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// self.tableView.reloadData()
return self.channels.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AllChannelTableViewCell

let strUrl = channels[indexPath.row].picture

cell.nameChannel.text = self.channels[indexPath.row].name
cell.urlChannel.text = self.channels[indexPath.row].url
cell.imageChannel.downloadFrom(url: URL(string: strUrl)!)

SVProgressHUD.dismiss()

return cell
}
}

最佳答案

您需要在设置完datasource 数组后重新加载tableView。此外,您目前正在 Alamofire block 外重新加载 tableView 但它应该在 Alamofire 请求 block 内。所以像这样更改您的重新加载代码。

func defaultChannel() {

if channels.count == 0 {

SVProgressHUD.show(withStatus: "dowload")

let URL = "http://52.50.138.211:8080/ChanelAPI/chanels"

Alamofire.request(URL).responseArray { (response: DataResponse<[AllCnannelModel]>) in

let channelArray = response.result.value

if let channelArray = channelArray {
for channel in channelArray {
try! self.realm.write() {
let newChannel = ChannelData()
newChannel.category_id = channel.category_id!
newChannel.idChannel = channel.id!
print(newChannel.idChannel)
newChannel.name = channel.name!
print(newChannel.name)
newChannel.picture = channel.picture
newChannel.url = channel.url!
print(newChannel.url)
self.realm.add(newChannel, update: true)
}
}
}
DispatchQueue.main.async{
//First set array
self.channels = self.realm.objects(ChannelData.self)
//Now reload the tableView
self.tableView.reloadData()
}
}

}
}

关于iOS:tableView.reloadData() 在 swift3 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42052893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com