2017-02-10 13 views
0

字典中讀取數據我想從包含圖像字典(或任何類型的對象)的字典中讀取數據。每個字典都有一個鍵(字符串)。iOS Swift從[字符串:[AnotherKindOfDictionary]]()

對於有些直觀的瞭解這就是我想實現:

userIdOne - > [圖像1,圖像2,圖像3,圖像4]

userIdTwo - > [圖像1,圖像2,圖像3]

userIdThree - > [圖像1,圖像2,圖像3,圖像4,圖像5]

userIdFour - > [圖像1,圖像2]

注意:這些圖像是n儘管擁有相同的「標題」,但仍有相同的圖像。他們只屬於每個個人用戶。 userId是[String:...,圖像字典是我在這個問題的標題中提到的[AnotherKindOfDictionary]。我想每個單元格中的每個userId和他們的圖像。總的來說,這將顯示4個單元格,但是當點擊時,它們的圖像將按順序顯示。

問題是我想把這些數據放在UITableView或UICollectionView中。無論如何,我都與之合作過。 類似於Snapchat的工作原理。無論何時單擊一個單元格,都會按順序顯示該用戶的圖像。

我已經能夠將數據加載到每個用戶ID是關鍵,但我在的CollectionView使用的數據有問題的字典(我目前的選擇,雖然我可以使用的tableView)

這裏是我的代碼:

var stories = [String : [StoryMedia]]() 

// StoryMedia is a struct containing info 

struct StoryMedia { 
    var storyMediaId: String? 
    var creatorId: String? 

    var datePosted: String? 

    var imageUrl: String? 

    init(storyMediaKey: String, dict: Dictionary<String, AnyObject>) { 
     storyMediaId = storyMediaKey 
     creatorId = dict["creatorId"] as? String 
     datePosted = dict["dateposted"] as? String 

     imageUrl = dict["imageUrl"] as? String 

    } 
} 


... Now in the actual viewController class UICollectionViewDataSource 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return stories.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let storyCell: StoryCell! 

    storyCell = collectionView.dequeueReusableCell(withReuseIdentifier: storyReuseIdentifier, for: indexPath) as! StoryCell 

    // What should I do here? 

    return storyCell 
} 

問題在於嘗試設置每個單元格。我無法通過其關鍵字獲取每個字典值,並將其用於每個單元格。

我已經嘗試使用:

// Failed attempt 1) 
let story = stories[indexPath.row] 
// but I get an ambiguous reference to member 'subscript' error 


// Failed attempt 2) 
for story in stories { 
    let creatorId = story.key 
    let sequenceOfStoryItems = story.value 


    for singleStoryItem in sequenceOfStoryItems { 

     // do something... 

    } 

} 

// but looping through an array for a collection view cell 
// does nothing to display the data and if I were to guess, 
// would be detrimental to memory if 
// I had a lot of "friends" or users in my "timeline" 

回答

0

字典是沒有順序的,所以它的尷尬使用,對於cellForItem功能(爲什麼你不能使用下標)。您可能能夠使用字典的值(即忽略鍵),但這可能是不同的B/N運行。我的意思是,你可以在stories.values數組中使用下標(不記得對「值」的確切要求,但它接近... allValues?...不確定,沒有辦法現在仔細檢查)而不是故事字典。

+0

我試着用let storyKeys = stories.keys。然後讓singleStoryKey = storyKeys [indexPath.row]。我仍然得到一個錯誤 –

+0

我不清楚你爲什麼看着鑰匙。cellForItem給你一個索引,所以在字典的值上使用該索引並忽略字典的鍵。你遇到了什麼錯誤? – ghostatron

+0

@ conarch關鍵是每個細胞。每個單元格代表一個用戶。字典中的每個鍵都是用戶的ID。從中我可以檢索他們的用戶名,profileImage等。值是所有故事項目(視頻,圖像,任何)的陣列。一旦單元被接通,該值將按順序「播放」。因此,我需要字典中每個數組的鍵以加載正確的故事數組 –

0

請問var stories = [String : [StoryMedia]]()需要是字典嗎? 字典沒有排序,所以不能像你問的那樣索引它們。你可以使它成爲一個元組數組嗎? var stories = [(username:String, media:StoryMedia)]()然後,您可以將您計劃存儲在原始密鑰中的任何值添加到元組的username:字段中。如果您更喜歡元組,則可以創建另一個具有usernamemedia屬性的結構。

通過調用簡單的stories.filter{}調用將個體usernamemedia結構從數組中取出應該是微不足道的。

+0

以前我已經成功完成了它,但它只適用於一個人。例如,如果我正在加載我的故事。但是我不能擁有多個用戶的tableview或collection視圖,每個用戶都包含他們自己的故事項目數組。從我嘗試過的東西幾乎肯定不能是一個簡單的StoryMedia數組。我不知道如何使用數組按順序爲每個用戶提取特定故事項目 –

+0

@ChrishonWyllie查看我的編輯 – dmorrow

+0

我以前從未使用過這種結構。但是,如果我是正確的,是不是需要我事先知道用戶名和確切的號碼?我基本上建立了一個時間表,新的項目在發佈時進入。我沒有事先知道用戶名 –