swift - 如何将数据从类模型填充到 TableView

标签 swift tableview swifty-json

我的数据通过创建其对象菜单进入名为 Menu 的模态类。现在我如何将菜单名称发送到表格 View 以在特定单元格中显示

 var menus = [Menu]()
                    for (_, content) in json {
                        let menu = Menu(id: Int(content["id"].stringValue),
                            name: content["name"].string,
                             image: content["image"].string,
                            coupon: content["coupon"].int,
                            icon: content["icon"].string,
                            order: Int(content["order"].stringValue),
                            aname: content["name"].string,
                            options: Int(content["options"].stringValue),
                            subcategory:content["subcategory"].arrayObject)

                        menus.append(menu)
                    }
                    for menu in menus {
                        print(menu.name)
                        print(menu.id)
                        print(menu.subcategory)
                    }
                    print(menus.count)

这里所有的数据都通过菜单对象的帮助保存在Menu类中。我添加了将数据显示到表格 View 的代码。在这里,我创建了自定义表格 View 并尝试填充它

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return menus.count

}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Menucell", forIndexPath: indexPath)as! MENUTableViewCell

    cell.Ordermenu.text = ("  \(menus[indexPath.row])")///here its not fetching the value
    return cell


    }

它不工作。应该如何实现?

enter image description here

它显示了projectName.classname

接受答案后更新 enter image description here

最佳答案

尝试下面的代码行。希望对您有帮助...

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Menucell", forIndexPath: indexPath)as! MENUTableViewCell
let menu : Menu =  menus[indexPath.row]
cell.Ordermenu!.text = menu. name
return cell


}

关于swift - 如何将数据从类模型填充到 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38075439/

相关文章:

json - 接收顶层带有括号的格式错误的 JSON

ios - 奇怪的 json 解码器行为

swift - Objective-C 枚举符合 RawRepresentable

arrays - 如何从字典数组中的所有同名键中检索值

javafx-2 - Javafx PropertyValueFactory 未填充 Tableview

ios - 如何使用json解析选择所有 TableView 行并获取所有行的id?

swift - 捏合手势识别器在第二次捏合缩放时重置图像

ios - Swift 自定义 UIViewController 的标签为 nil

ios - 为什么 editActionsForRowAt 返回很多图像而不是一张图像?

swift - 我的 API 请求代码产生了一个我无法调试或理解的错误