【问题标题】:IOS Swift reading data from a dictionary of [String: [AnotherKindOfDictionary] ] ( )IOS Swift 从 [String: [AnotherKindOfDictionary] ] ( ) 的字典中读取数据
【发布时间】:2017-02-10 17:14:59
【问题描述】:

我想从包含图像字典(或任何类型的对象)的字典中读取数据。每个字典都有一个键(字符串)。

为了有点直观的理解,这是我想要实现的目标:

userIdOne -> [image1, image2, image3, image4]

userIdTwo -> [image1, image2, image3]

userIdThree -> [image1, image2, image3, image4, image5]

userIdFour -> [image1, image2]

注意:尽管“标题”相同,但这些图片并不是同一张图片。它们只属于每个单独的用户。 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"

【问题讨论】:

    标签: ios swift uitableview dictionary uicollectionview


    【解决方案1】:

    字典没有顺序,因此将它用于 cellForItem 函数很尴尬(以及为什么不能使用该下标)。您也许可以使用字典的值(即忽略键),但这可能是不同的 b/n 运行。我的意思是,您可以在 stories.values 数组上使用下标(不记得对“值”的确切调用,但它很接近...allValues?...不确定,没有办法现在仔细检查)而不是故事字典。

    【讨论】:

    • 我尝试过使用 let storyKeys = stories.keys。然后让 singleStoryKey = storyKeys[indexPath.row]。我仍然收到错误
    • 我不清楚您为什么要查看 key。 cellForItem 为您提供了一个索引,因此在字典的值上使用该索引并忽略字典的键。你遇到了什么错误?
    • @conarch 关键是每个单元格。每个单元格代表一个用户。字典中的每个键都是用户的 id。我可以从中检索他们的用户名、profileImage 等。该值是所有故事项目(视频、图像等)的数组。一旦点击单元格,此值将按顺序“播放”。因此,我需要字典中每个数组的键才能加载正确的故事数组
    【解决方案2】:

    var stories = [String : [StoryMedia]]() 需要是字典吗? 字典没有排序,所以不能像你问的那样索引它们。你能把它变成一个元组数组吗? var stories = [(username:String, media:StoryMedia)]() 然后,您可以将计划存储在原始键中的任何值添加到元组的 username: 字段中。如果您更喜欢元组,则可以创建另一个具有 usernamemedia 属性的结构。

    通过简单的stories.filter{} 调用将单个usernamemedia 结构拉出数组应该很简单。

    【讨论】:

    • 我之前已经成功地做到了,但它只对一个人有效。例如,如果我只加载我的故事。但是我不能拥有一个包含多个用户的表格视图或集合视图,每个用户都包含他们自己的故事项目数组。根据我的尝试,几乎可以肯定它不可能是一个简单的 StoryMedia 数组。我不知道如何使用数组按顺序为每个用户提取特定的故事项目
    • 我以前从未使用过这种结构。但是如果我是正确的,那不需要我事先知道用户名和确切的数字吗?我基本上是在建立一个时间表,其中更新的项目在发布时就会出现。我事先不知道用户名
    • @ChrishonWyllie 我认为您需要在这里分享更多详细信息。你是如何获取数据的?您事先知道用户名吗?你的显示器是什么样的?您始终可以附加到您的数组。或者,您可以以您拥有的格式存储数据,然后使用 stories.keys 数组创建用户名数组。一旦你有了一组用户名,就可以用密钥从故事中挑选数据了。
    猜你喜欢
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    相关资源
    最近更新 更多