簡體   English   中英

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
    )
)

但是在我的標簽上,我得到了“飲料”。 而已。 如何獲得其他2個值(如果大於或小於3個值,則取X值)?

我的for in loop顯然沒有解決問題。 假設我可以優化/修復該問題以顯示所有值?

提前致謝。

在循環中,您要多次設置標簽文本。 每次設置它都不會累積,它會用新文本完全替換當前文本。 您會想要這樣的東西:

    // 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]] 我假設您的表格視圖中的每個單元格代表數組中的字符串數組之一。 因此,每個單元格代表一個[String] 因此,您的items應為arrayExample[indexPath.row] AnyObjectAnyObject沒有太大意義。 如果有什么需要將其強制轉換為[[AnyObject]] ,則沒有理由因為編譯器應該已經知道它是[[String]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM