cellForRowAtIndexPath 中的多维数组循环 Swift

Multidimensional Array Looping in cellForRowAtIndexPath Swift

我有一个多维数组,我想将其值显示到每个相应单元格中的一个 UILabel 上。

我的数组是这样的:

var arrayExample = [["beverages", "food", "suppliers"]["other stuff, "medicine"]]

我在 cellForRowAtIndexPath 中循环遍历这些值,以便它在不同的单元格(在 UILabel 上)显示适当的值:

if let onTheLabel: AnyObject = arrayOfContactsFound as? AnyObject {

                for var i = 0; i < objects!.count; i++ {


                    cell?.contactsUserHas.text = "\(onTheLabel[indexPath.row][i])" as! String

                    print("arrayOfContactsFound Printing! \(onTheLabel)")

                }

            }

打印到控制台时我得到:

arrayOfContactsFound Printing! (
        (
        beverages,
        "supply chain",
        pharmacuticals
    )
)

但是在我的标签上我得到 "beverages"。而已。如何获得其他 2 个值(如果多于或少于 3 个值,则为 X 量)?

我的 for in loop 显然没有成功。假设我可以 optimize/fix 显示所有值?

提前致谢。

在您的循环中,您多次设置标签的文本。每次设置它都不会累积,它会用新文本完全替换当前文本。你会想要这样的东西:

    // Remove the cast and the loop in your code example, and replace with this
    let items = arrayOfContactsFound[indexPath.row]
    let itemsString = items.joinWithSeparator(" ")
    cell?.contactsUserHas.text = itemsString

另一件需要注意的事情是你的演员阵容没有多大意义。

var arrayExample = [["beverages", "food", "suppliers"]["other stuff, "medicine"]]

所以 arrayExample 是类型 [[String]]。我假设 table 视图中的每个单元格代表数组中的字符串数组之一。所以每个单元格代表一个[String]。所以你的 items 应该是 arrayExample[indexPath.row]。强制转换为 AnyObject 没有太大意义。如果你想将它转换为 [[AnyObject]],但没有理由这样做,因为编译器应该已经知道它是 [[String]].