Ios 使用字典数组在Swift中填充tableview

Ios 使用字典数组在Swift中填充tableview,ios,uitableview,swift,Ios,Uitableview,Swift,我创建了一个字典数组,如下所示: var menuItems = [["Image" : "bars_icon_main_page", "Title" : "Bars"], ["Image" : "clubs_icon_main_page", "Title" : "Clubs"]] func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableV

我创建了一个字典数组,如下所示:

    var menuItems = [["Image" : "bars_icon_main_page", "Title" : "Bars"], ["Image" : "clubs_icon_main_page", "Title" : "Clubs"]]
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     var cell: MenuTableViewCell = tableView.dequeueReusableCellWithIdentifier("MenuCell") as MenuTableViewCell

     let dict = menuItems[indexPath.row]
     cell.menuImage.image = UIImage(named: dict["Image"]!)
     cell.menuTitle.text = dict["Title"]

     return cell
    }
我创建了一个自定义表视图单元格,并按如下方式填充表视图单元格:

    var menuItems = [["Image" : "bars_icon_main_page", "Title" : "Bars"], ["Image" : "clubs_icon_main_page", "Title" : "Clubs"]]
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
     var cell: MenuTableViewCell = tableView.dequeueReusableCellWithIdentifier("MenuCell") as MenuTableViewCell

     let dict = menuItems[indexPath.row]
     cell.menuImage.image = UIImage(named: dict["Image"]!)
     cell.menuTitle.text = dict["Title"]

     return cell
    }
在调试src代码时,在第行下面执行之后,“dict”是nil


我搞不清问题出在哪里。

下面的代码行正在查找
图像的键。

cell.menuImage.image = UIImage(named: dict["Image"]!)
但是
menuItems
正在使用
Image:
作为键:

var menuItems = [["Image:" : "bars_icon_main_page", "Title" : "Bars"], ["Image:" : "clubs_icon_main_page", "Title" : "Clubs"]]
我怀疑您不打算在密钥中包含冒号:

var menuItems = [["Image" : "bars_icon_main_page", "Title" : "Bars"], ["Image" : "clubs_icon_main_page", "Title" : "Clubs"]]

您的字典数组正在使用Image:键实现,您的代码正在使用Image而不使用

谢谢。这是个打字错误。我更新了我的问题。问题出在“let dict=menuItems[indexPath.row]”行中。您是否验证了indexPath.row是否包含在数组中?>>像
if(menuItems.count>indexath.row){do your logic…}
@VictorCarvalhoTavernari如果你尝试访问数组范围之外的内容,那么它不应该崩溃吗?不,使用集合是脆弱的,如果你尝试访问数组范围之外的内容,你的应用程序将崩溃。处理收藏时请注意。谢谢。这是个打字错误。我更新了我的问题。但问题在于“let dict=menuItems[indexPath.row]”行@Satyam显然,根据您在问题中提供的内容,
dict
不可能是
nil
。调试器可能会混淆(因此以编程方式
println
value,不要依赖调试器)。和
println
三件事:1
println(indexPath.row)
?2.
println(menuItems)
(确保某些内容没有更改它或弄乱值);3
println(dict)
.indexPath.row正在打印0和1。menuItems正在打印整个阵列。dict值被打印为{}很明显这里还发生了其他事情。也许你可以创建一个程序并上传到某个我们可以查看的地方。程序崩溃了吗?有错误消息吗?@alkku,由于dict值为零,下一行将崩溃。