我的self.tableView重载数据函数由于某种原因未被调用。这是我的整个tableview代码:
class NewMessageController: UITableViewController {
let currentUser: String = (FIRAuth.auth()?.currentUser?.uid)!
var ref = FIRDatabaseReference()
let cellId = "cellId"
var users = [User]()
var groupUserArray = [GlobalVariables.UserArray]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
getCurrentUserSchoolOrWorkAddressAndDisplayOtherUsers()
navigationItem.title = "New Message"
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancel", style: .Plain, target: self, action: #selector(cancelButton))
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Select", style: .Plain, target: self, action: #selector(selectButton))
// fetchUserAndDisplay()
tableView.registerClass(UserCell.self, forCellReuseIdentifier: cellId)
}
var messagesController2 = MessagingViewController?()
func selectButton() {
tableView.allowsMultipleSelectionDuringEditing = true
tableView.setEditing(true, animated: false)
if tableView.allowsMultipleSelectionDuringEditing == true {
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Create", style: .Plain, target: self, action: #selector(createGroupMessagesButton))
}
}
func cancelButton() {
if tableView.allowsMultipleSelectionDuringEditing == true {
tableView.allowsMultipleSelectionDuringEditing = false
tableView.setEditing(false, animated: true)
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Select", style: .Plain, target: self, action: #selector(selectButton))
}
else {
dismissViewControllerAnimated(true, completion: nil)
}
}
func fetchUserAndDisplay() {
FIRDatabase.database().reference().child("users").observeEventType(.ChildAdded, withBlock: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.id = snapshot.key
user.setValuesForKeysWithDictionary(dictionary)
self.users.append(user)
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}, withCancelBlock: nil)
}
func createGroupMessagesButton() {
dismissViewControllerAnimated(true) {
if let selectedUserRows = self.tableView.indexPathsForSelectedRows {
self.groupUserArray.append(selectedUserRows)
for index in selectedUserRows {
let text = self.users[index.row]
self.messagesController?.showChatLogController(text)
}
}
}
}
func getCurrentUserSchoolOrWorkAddressAndDisplayOtherUsers() {
let currentUser = (FIRAuth.auth()?.currentUser!)
let userID = currentUser?.uid
FIRDatabase.database().reference().child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
let schoolOrWorkAddress = snapshot.value!["schoolOrWorkAddress"] as! String
FIRDatabase.database().reference().child("schoolOrWorkAddress").child(schoolOrWorkAddress).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
if(!snapshot.exists()){
return
}
let locations = snapshot.value! as! NSDictionary
for (index, location) in locations.enumerate() {
FIRDatabase.database().reference().child("users").child(location.key as! String).observeEventType(.ChildAdded, withBlock: { (snapshot: FIRDataSnapshot) in
if(snapshot.exists()){
print(snapshot)
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.id = snapshot.key
user.setValuesForKeysWithDictionary(dictionary)
self.users.append(user)
print(self.users)
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}
}, withCancelBlock: nil)
}
})
})
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return users.count
}
var messagesController = MessagingViewController?()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView.allowsMultipleSelectionDuringEditing != true {
dismissViewControllerAnimated(true) {
let user = self.users[indexPath.row]
self.messagesController?.showChatLogController(user)
}
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! UserCell
let user = users[indexPath.row]
cell.textLabel?.text = user.fullName
cell.detailTextLabel?.text = user.email
if let userPhoto = user.userPhoto {
cell.profileImageView.loadImageUsingCacheWithUrlString(userPhoto)
}
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 72
}
}
然而问题就出现在这里: for(index,location)in locations.enumerate(){
FIRDatabase.database().reference().child("users").child(location.key as! String).observeEventType(.ChildAdded, withBlock: { (snapshot: FIRDataSnapshot) in
if(snapshot.exists()){
print(snapshot)
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = User()
user.id = snapshot.key
user.setValuesForKeysWithDictionary(dictionary)
self.users.append(user)
print(self.users)
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}
}, withCancelBlock: nil)
}
由于某种原因,没有调用上面的reloadData函数!
以下是断点图片:
有没有人想过为什么会这样? 任何帮助将不胜感激