iOS:tableView.reloadData()不';我不能在迅捷工作

iOS:tableView.reloadData()不';我不能在迅捷工作,ios,swift,uitableview,alamofire,reloaddata,Ios,Swift,Uitableview,Alamofire,Reloaddata,我是Swift新手,我试图在Swift中更新数据后重新加载我的表视图,但它似乎不起作用。当我更改选项卡并返回时,tableView会重新加载,但不会自动加载。这是我的密码: 我的TableViewController: class AllChannelTableViewController: UITableViewController { var modelChannel = [AllCnannelModel]() let realm = try! Realm()

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

我的TableViewController:

 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
    }
}
类AllChannelTableViewController:UITableViewController{
var modelChannel=[AllCnannelModel]()
让realm=try!realm()
惰性变量通道:结果={self.realm.objects(ChannelData.self)}()
重写func viewDidLoad(){
super.viewDidLoad()
defaultChannel()
}
func defaultChannel(){
如果channels.count==0{
SVProgressHUD.show(状态为“dowload”)
让URL=”http://52.50.138.211:8080/ChanelAPI/chanels"
请求(URL).responseArray{(响应:DataResponse)在
让channelArray=response.result.value
如果让channelArray=channelArray{
用于通道阵列中的通道{
试试!self.realm.write(){
设newChannel=ChannelData()
newChannel.category\u id=channel.category\u id!
newChannel.idChannel=channel.id!
打印(newChannel.idChannel)
newChannel.name=channel.name!
打印(newChannel.name)
newChannel.picture=channel.picture
newChannel.url=channel.url!
打印(newChannel.url)
self.realm.add(newChannel,update:true)
}
}
}
}
DispatchQueue.main.async{
self.tableView.reloadData()
self.channels=self.realm.objects(ChannelData.self)
}
}
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
}
//标记:-表视图数据源
重写func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
//self.tableView.reloadData()
返回self.channels.count
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为“cell”,for:indexath)作为!AllChannelTableViewCell
设strUrl=channels[indexPath.row].picture
cell.nameChannel.text=self.channels[indexath.row].name
cell.urlChannel.text=self.channels[indexath.row].url
cell.imageChannel.downloadFrom(url:url(string:strUrl)!)
SVProgressHUD.disclose()
返回单元
}
}

设置
数据源
数组后,需要重新加载
表视图
。此外,您当前正在
Alamofire
块外部重新加载
tableView
,但它应该在
Alamofire
请求块内部。所以,像这样更改重新加载代码

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()
            }
        }

    }
}
func defaultChannel(){
如果channels.count==0{
SVProgressHUD.show(状态为“dowload”)
让URL=”http://52.50.138.211:8080/ChanelAPI/chanels"
请求(URL).responseArray{(响应:DataResponse)在
让channelArray=response.result.value
如果让channelArray=channelArray{
用于通道阵列中的通道{
试试!self.realm.write(){
设newChannel=ChannelData()
newChannel.category\u id=channel.category\u id!
newChannel.idChannel=channel.id!
打印(newChannel.idChannel)
newChannel.name=channel.name!
打印(newChannel.name)
newChannel.picture=channel.picture
newChannel.url=channel.url!
打印(newChannel.url)
self.realm.add(newChannel,update:true)
}
}
}
DispatchQueue.main.async{
//第一集数组
self.channels=self.realm.objects(ChannelData.self)
//现在重新加载tableView
self.tableView.reloadData()
}
}
}
}

设置
数据源
数组后,需要重新加载
表视图
。此外,您当前正在
Alamofire
块外部重新加载
tableView
,但它应该在
Alamofire
请求块内部。所以,像这样更改重新加载代码

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()
            }
        }

    }
}
func defaultChannel(){
如果channels.count==0{
SVProgressHUD.show(状态为“dowload”)
让URL=”http://52.50.138.211:8080/ChanelAPI/chanels"
请求(URL).responseArray{(响应:DataResponse)在
让channelArray=response.result.value
如果让channelArray=channelArray{
用于通道阵列中的通道{
试试!self.realm.write(){
设newChannel=ChannelData()
newChannel.category\u id=channel.category\u id!
newChannel.idChannel=channel.id!
打印(newChannel.idChannel)
newChannel.name=channel.name!
打印(newChannel.name)
newChannel.picture=channel.picture
newChannel.url=channel.url!
打印(newChannel.url)
self.realm.add(newChannel,update:true)
}
}
}
DispatchQueue.main.async{
//