首页 文章

为每个TableViewCell实例化新数组

提问于
浏览
0

The objective

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

The Problem

  • 每次为每个单独的JSON对象附加到字符串数组时,附加不会附加到每个单独单元格的数组中;附加是针对整个JSON对象的整个提要,因此最终得到了正在被提取的每个JSON对象的数组 . (我需要为每个单独的单元格使用单独的数组)

例如=

  • cell 1 = [“你好”,“谢谢你”,“欢迎”]

  • cell 2 = [“美国”,“中国”,“俄罗斯”]

  • cell 3 = [“Patatoe”,“Orange”,“Apple”]

  • 等......

What I'm Doing?

  • 我从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)
                        })
                    })
                })

            })
        }
    })

What Have I Tried?

我试过了:

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

  • 创建一个数组值的字典,并且(不成功)将数组附加到每个键(indexPath.row) .

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

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

2 回答

  • 0

    你的问题需要澄清,但读了两遍,我觉得你想要维护阵列数组,这可以通过以下方式完成 .

    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"]]
    
  • 0

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

    这篇文章过于复杂 .

相关问题