单击TabBarButton后无法设置我的tableView单元格

时间:2017-05-17 17:20:51

标签: swift

我的应用程序有一个带有两个项目的TabBarViewController:一个按钮的标题是“RICHIESTE”,另一个是“IN VENDITA”。当我点击这两个按钮时,我执行以下指示:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    if(item.title == "RICHIESTE"){

        let myUrl = NSURL(string:"http://chuadiv.ddns.net/easytoschool/fetch_book_user_request.php");

        let request = NSMutableURLRequest(url:myUrl as! URL);
        request.httpMethod = "POST";
        let postString = "mail=\(UserDefaults.standard.object(forKey: "userEmail") as! String)";
        request.httpBody = postString.data(using: String.Encoding.utf8);
        MenuViewController.requestBooksArray.removeAll()

        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in

            if error != nil{
                print("error=\(error)")
                return
            }

            var err: NSError?

            do{
                var json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray


                if let parseJSON: NSArray = json {

                    for index in 0...parseJSON.count-1 {

                        if (parseJSON[index] is NSNull){
                            DispatchQueue.main.async {
                                var myAlert = UIAlertController(title:"Attenzione\n", message:"Nessun libro trovato", preferredStyle:UIAlertControllerStyle.alert);

                                let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default){ action in }

                                myAlert.addAction(okAction);
                                self.present(myAlert, animated:true, completion:nil);
                            }
                        }else{
                            let libro = parseJSON[index] as! [String:Any]
                            print("\n\n",index,":\n")

                            let book = requestBook.init(bookId: libro["id"] as! String,bookName: libro["name"] as! String,bookAuthor: libro["author"] as! String,bookPrice: libro["price"] as! String,bookStatus: libro["status"] as! String,bookISBN: libro["isbn"] as! String,bookType: libro["type"] as! String,bookIdRequester: libro["idRequester"] as! String,bookNameRequester: libro["nameRequester"] as! String,bookSurnameRequester: libro["surnameRequester"] as! String,bookMailRequester: libro["mailRequester"] as! String,bookNumberStatusRequester: libro["numberStatusRequester"] as! String,bookRequestedOn: libro["requestedOn"] as! String)

                            book.printBook()

                            MenuViewController.requestBooksArray.append(book)

                        }
                    }
                }
            }catch{
                print("error=\(error)")
                return
            }
        }
        task.resume();

    }else{
            let myUrl = NSURL(string:"http://chuadiv.ddns.net/easytoschool/fetch_book_user.php");

            let request = NSMutableURLRequest(url:myUrl as! URL);
            request.httpMethod = "POST";
            let postString = "mail=\(UserDefaults.standard.object(forKey: "userEmail") as! String)";
            request.httpBody = postString.data(using: String.Encoding.utf8);
            TabBarViewController.sellBooksArray.removeAll()

            let task = URLSession.shared.dataTask(with: request as URLRequest){
                data, response, error in

                if error != nil{
                    print("error=\(error)")
                    return
                }

                var err: NSError?

                do{
                    var json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray


                    if let parseJSON: NSArray = json {

                        for index in 0...parseJSON.count-1 {

                            if (parseJSON[index] is NSNull){
                                DispatchQueue.main.async {
                                    var myAlert = UIAlertController(title:"Attenzione\n", message:"Nessun libro in vendita", preferredStyle:UIAlertControllerStyle.alert);

                                    let okAction = UIAlertAction(title:"Ok", style:UIAlertActionStyle.default){ action in }

                                    myAlert.addAction(okAction);
                                    self.present(myAlert, animated:true, completion:nil);
                                }
                            }else{
                                let libro = parseJSON[index] as! [String:Any]
                                print("\n\n",index,":\n")

                                let book = sellBook.init(bookId: libro["id"] as! String,bookName: libro["name"] as! String,bookAuthor: libro["author"] as! String,bookPrice: libro["price"] as! String,bookStatus: libro["status"] as! String,bookISBN: libro["isbn"] as! String,bookType: libro["type"] as! String,bookAddedOn: libro["addedOn"] as! String)

                                book.printBook()

                                TabBarViewController.sellBooksArray.append(book)

                            }
                        }
                    }
                }catch{
                    print("error=\(error)")
                    return
                }
            }
            task.resume();
    }
}

执行这些说明后,我想根据按钮设置两个TableView。 我不能这样做,因为当我点击TabBarController中的按钮时,它立即进入TableView然后执行指令,因此它不设置任何行。 有人能帮助我吗?

0 个答案:

没有答案