TableView数据未重新加载Swift

TableView数据未重新加载Swift,swift,swift3,Swift,Swift3,我正在使用餐厅应用程序,需要获取所有餐厅类型。我已成功获取所有数据,但tableview未重新加载 var arrSubMenu = [ResataurantType]() //TableView Datasource And Delegate Method func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print(arrSubMenu.count)

我正在使用餐厅应用程序,需要获取所有餐厅类型。我已成功获取所有数据,但tableview未重新加载

var arrSubMenu = [ResataurantType]()

//TableView Datasource And Delegate Method
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(arrSubMenu.count)
    return arrSubMenu.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: leftMenuTableViewCell = 
    tableView.dequeueReusableCell(withIdentifier: "tableCell") as! 
    leftMenuTableViewCell
    cell.name.text = self.arrSubMenu[indexPath.row].type
    return cell
}

func getRestaurantType() {
    let manager = AFHTTPSessionManager()
    manager.requestSerializer = AFJSONRequestSerializer()
    manager.get(RESTAURANTTYPE, parameters: nil, progress: nil, success: { 
        (operation, responseObj) in
        if let objDic = responseObj as? [String:Any] {
            if let objArray = objDic["RESTAURANT_TYPE"] as? NSArray {
                for objType in objArray {
                    let ObjRestaurant = ResataurantType()
                    if let objString = objType as? String {
                        ObjRestaurant.type = objString
                    }
                    self.arrSubMenu.append(ObjRestaurant)
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }) { (operation, error) in
        print(error)
    }
}

我在ViewDidLoad中调用了这个函数,但仍然无法用record处理tableview,这可能是我个人经历的原因

TableView的数据源未设置为self。 -验证:-tableView数据源和委托方法上的断点

子菜单数组不包含单个值。 -验证:-断点&在重新加载tableView之前打印子菜单


您只需输入:

首先是一个出口:

@IBOutlet var tableView : UITableView
在viewDidLoad中:

tableView.dataSource = self //OR connection between tableView in storyboard and tableView in swift class 

您何时调用getRestaurantType?是否会在分配tableview的数据源之前调用它?这可能会使tableview显示为空,尽管底层数据存在。并且,除非您在程序中的某个其他点调用reloadData,否则它不会刷新自身。

将下面的at单元格替换为row at index方法

let cell = 
tableView.dequeueReusableCell(withIdentifier: "tableCell" ,for: indexPath)as! 
leftMenuTableViewCellenter 

您将tableView的委托和数据源设置为您的此类了吗?@RakshithNandish是的,我已经设置了..我得到了arrSubMenu.count总计numberOfRowsInSection但cellForRowAt未执行..arrSubMenu.count可能是0。@OzgurVatansever我得到了arrSubMenu.count但cellForRowAt未执行..事实上,如果我得到numberOfRowsInSection为何cellForRowAt未执行感谢您的回复,但数据源连接正常。如果我使用静态数组,则所有操作都可以正常工作,但当我从API获取数据时,它不工作。。