【问题标题】:Instantiate new array for each TableViewCell为每个 TableViewCell 实例化新数组
【发布时间】:2017-07-31 04:27:52
【问题描述】:

目标

  • 为每个单独的表格视图单元格创建一个新的字符串数组。

问题

  • 每次我为每个单独的 JSON 对象追加到字符串数组时,追加不会追加到每个单独单元格的数组;附加是针对整个 JSON 对象的提要,因此,最终会得到一个包含正在获取的每个 JSON 对象的数组。(我需要为每个单独的单元格创建一个单独的数组)

    例如 =

  • 单元格 1 = [“你好”、“谢谢”、“欢迎”]

  • 单元格 2 = [“美国”、“中国”、“俄罗斯”]

  • 单元格 3 = ["Patatoe", "Orange", "Apple"]

  • 等等……

我在做什么?

  • 我正在从 firebase 中提取一个字符串数组,并将这些值附加到一个在 cellForRowAtIndexPath 方法中实例化的空数组中。

代码

 var peopleWhomAreInvited = [String]()
        
    Database.database().reference().child("following").child((currentUser?.uid)!).observe(.childAdded, with: { (snapshot) in
        Database.fetchUserWithUID(uid: snapshot.key) { (user) in
            let allUserRef =  Database.database().reference().child("plans").child((user.uid))
            
            allUserRef.observeSingleEvent(of: .value, with: { (snapshot) in
                guard let dictionaries = snapshot.value as? [String: Any] else { return }
                dictionaries.forEach({ (key, value) in
                    
                    let peopleAttendingRef = allUserRef.child(key).child("invited").observe(.childAdded, with: { (peopleSnapshot) in
                        let personsRef = allUserRef.child(key).child("invited").child(peopleSnapshot.key)
                        personsRef.observe(.childAdded, with: { (personSnapshot) in
                            guard let peopleAttendingDictionary = personSnapshot.value as? String else { return }
                            peopleWhomAreInvited.append(peopleAttendingDictionary)
                            print("THIS IS WHO I INVITED", peopleWhomAreInvited)
                        })
                    })
                })
                
            })
        }
    })

我尝试了什么?

我试过了:

  • 在 cellForRowAtIndexPath 方法之外和在单元格中声明数组

  • 创建一个具有数组值的字典,并(未成功)将该数组附加到每个键(即 indexPath.row)。

  • 在单元格内而不是在 cellForRowAtIndexPath 中声明 fetch 方法

我不知道该怎么做...如果您有任何想法,请告诉我。

【问题讨论】:

  • 二维数组可能对您有所帮助。
  • 你在哪里使用数组?解释有点混乱。此外,peopleWhomAreInvited 应该是谁而不是谁。动词“to be”采用主格。
  • 让你的问题有点清晰易懂

标签: ios arrays swift uitableview dictionary


【解决方案1】:

您的问题需要澄清,但读了两次,我觉得您想要维护 Array of Arrays,这可以通过以下方式完成。

var arrayForAppending = [[String]]()

let cellOneArray = ["hello", "thank you", "welcome"]
let cellTwoArray = ["America", "China", "Russia"]
let cellThreeArray = ["Patatoe", "Orange", "Apple"]

arrayForAppending.append(cellOneArray)
arrayForAppending.append(cellTwoArray)
arrayForAppending.append(cellThreeArray)
print(arrayForAppending)

// Print Output
// [["hello", "thank you", "welcome"], ["America", "China", "Russia"], ["Patatoe", "Orange", "Apple"]]

【讨论】:

  • 我尽量让它问清楚。这仍然不能回答我的需要。对于每个单独的单元格(在表格视图中),它不会返回一个新数组。
【解决方案2】:

解决此问题的方法是将数组添加到保存每个帖子的值的结构中。实例化结构体时在每个单元格上创建一个新的 post 对象,例如 var post = Post(dictionary: dictionary)

这篇文章过于复杂了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 2021-11-02
    • 2021-01-09
    • 2013-01-03
    • 1970-01-01
    • 2013-11-07
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多