2017-07-31 185 views
0

目的实例化每个TableViewCell

  • 新阵列创建串的每个单独的表格视图单元格的新数组。

的问题

  • 每次追加到字符串数组为每个单独的JSON对象,附不附加到该阵列为每个单独的小区;所附为JSON对象的整个整个进料,因此与正被取出的每JSON对象的阵列结束了..(我需要一个单独的阵列为每个单独的小区)

    例如=

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

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

  • 小区3 = [” Patatoe“,”Orange“,”Apple“]

  • 等...

我在做什么?

  • 我拉动从火力字符串数组和附加的值到的cellForRowAtIndexPath方法内实例化的空数组。

CODE

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

我不知道该怎么办......如果您有任何想法,请让我知道。

+0

2D阵列可能会对您有所帮助。 –

+0

你在哪里使用数组?解释有点混乱。此外,人们对未受邀的人应该是谁而不是谁。动词'是'代表主格。 –

+0

让你的问题有点清楚了解 –

回答

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

我试图让问题尽可能清楚。这仍然不能回答我所需要的。对于每个单独的单元格(在表格视图中)它不返回一个新的数组。 –

0

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

这篇文章过于复杂。