表视图使用Firestore在iOS Swift中显示重复数据?

时间:2018-06-11 09:17:32

标签: ios swift

这里我使用的是firestore数据库。我将用户表数据提取到表视图单元格中。在表格视图中列出具有跟随按钮选项的所有用户。如果用户第一次运行表视图不重复数据,当用户关闭应用程序并再次返回时,转到人员建议选项卡列出所有用户并且其所有用户都是重复的。

以下是截图:enter image description here

 import UIKit

 class peopleViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var users: [Users] = []

override func viewDidLoad() {
    super.viewDidLoad()

    title = "People"
    loadUsers()

}



func loadUsers() {


    API.User.observeUser { (user) in
        self.isFollowing(userId: user.id!, completed: { (value) in
            user.isFollowing = value

            self.users.append(user)
            self.tableView.reloadData()

        })
    }

}


func isFollowing(userId: String, completed: @escaping (Bool) -> Void) {
    API.Follow.isFollowing(userId: userId, completed: completed)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ProfileSegue" {
        let profileVC = segue.destination as! UserViewController
        let userId = sender  as! String
        profileVC.userId = userId
        profileVC.delegate = self as? UserViewControllerDelegate
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return users.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleTableViewCell", for: indexPath) as! peopleTableViewCell


    let user = users[indexPath.row]
    print("letuser::::\(user)")
    cell.user = user
    cell.delegate = self
    return cell
}





 }
  extension peopleViewController: PeopleTableViewCellDelegate {
  func goToProfileUserVC(userId: String) {
    performSegue(withIdentifier: "ProfileSegue", sender: userId)


 }
}

以下是从firestore获取数据的代码:

     func observeUser(completion: @escaping (Users) -> Void) {

    db.collection("users")
        .getDocuments() { (querySnapshot, err) in
            if let err = err {
                print("Error getting documents: \(err)")
            } else {

                self.userList.removeAll()

                var user : Users?
                for document in querySnapshot!.documents {

                     user = Users.transformUser(postDictionary: document.data(), key: document.documentID)
    //                        self.userList.append(user!)
                    completion(user!)
                }

    //                    completion(user!)

            }
    }
}

2 个答案:

答案 0 :(得分:2)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layoutDirection="rtl" android:textDirection="rtl"> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout2"> <ImageView android:src="@drawable/pause" android:id="@+id/imgRating" android:layout_width="70dp" android:layout_height="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" /> <ImageView android:src="@drawable/dapao" android:id="@+id/imgThumbnail" android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" /> <TextView android:text="10,000 تومان" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textStyle="bold" android:id="@+id/foodPrice" android:layout_marginLeft="5dp" android:textDirection="rtl" /> </LinearLayout> <View android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginRight="20dp"> <TextView android:text="نام غذا" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtFoodName" android:textSize="20sp" android:textStyle="bold" /> <TextView android:text="توضیحات غذا" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="12sp" android:id="@+id/txtFoodDesc" /> <TextView android:text="تاریخ" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/txtDate" android:textSize="8sp" android:textAllCaps="false" /> <TextView android:text="آشپز" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/txtCook" android:textSize="8sp" android:textAllCaps="false" /> </LinearLayout> </LinearLayout> 行写loadUsers()

之前的 {

如果那不是问题那么你必须从数组中删除所有重复项。

要做到这一点,你必须创建一个删除重复项的func e.g。

self.users.append(user)

然后在重新加载表格视图之前调用func

答案 1 :(得分:1)

因为您要向阵列追加相同的数据。您可以尝试在 loadUsers 方法中清除阵列吗?

   func loadUsers() {

        self.users = []

        API.User.observeUser { (user) in
            self.isFollowing(userId: user.id!, completed: { (value) in
                user.isFollowing = value

                self.users.append(user)
                self.tableView.reloadData()

            })
        }
    }