我使用以下代码填充数组,然后让该数组的计数确定行数。但是,赋值语句由于某种原因继续迭代,并且表视图永远不会出现。
代码:
import Foundation
import UIKit
import Contacts
import ContactsUI
import Firebase
import SendBirdSDK
class Profile: UIViewController, CNContactPickerDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var myTableView: UITableView!
var name = [String]()
var phoneNo = [String] ()
var user1cred = ""
var user2cred = ""
override func viewDidLoad() {
//put code below
super.viewDidLoad()
let ref = FIRDatabase.database().reference()
let nib = UINib(nibName: "ContactCell", bundle: nil)
self.myTableView.register(nib, forCellReuseIdentifier: "cell")
let myuser = FIRAuth.auth()?.currentUser
let contactRef = ref.child("Users").child((myuser?.uid)!).child("Contacts")
contactRef.observe(.value, with: {(snapshot) in
//creates enumerator of snapshot children
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
//adds the phone numbers of users contacts to array
self.phoneNo.append(rest.key )
self.name.append(rest.value as! String)
}
print("Below is size of phoneNo")
print(self.phoneNo.count)
print("Below is name:")
print(self.name)
print("below is phone no")
print(self.phoneNo)
})
//self.myTableView.reloadData() //<<<<<<<<<<<<< RELOADS TABLEVIEW
} //end of viewDidLoad()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("test coiunt")
print(phoneNo.count)
print("The number of sections is: " + String(phoneNo.count))
return phoneNo.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ContactCell
//below gets the index
let val = indexPath.row
//below assigns the values of the fields on the cell
cell.contactName.text = (name[val])
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//below sets the value for the cell height
return 90.0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//below prints the name of the selected user
print(name[indexPath.row])
//get the users to pass into below function
let ref = FIRDatabase.database().reference()
let myuser = FIRAuth.auth()?.currentUser
var contactNum = ""
let userInfo = ref.child("Users").child((myuser?.uid)!)
userInfo.observe(.value, with: {(snapshot) in
//get the current users credentials
self.user1cred = snapshot.childSnapshot(forPath: "/Email").value as! String
let user1PW = snapshot.childSnapshot(forPath: "/Password").value as! String
})
let user2Name = name[indexPath.row]
let user2Info = ref.child("Users").child((myuser?.uid)!).child("Contacts")
user2Info.observe(.value, with: {(snapshot) in
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? FIRDataSnapshot {
print(rest.value as! String)
print(user2Name)
if((rest.value as! String) == user2Name) {
//this means that it has iterated to the correct user
self.user2cred = rest.key
} else {
//do NOTHING this is the wrong user being iterated on
}
}
})
/*
//below inserts a new cell into the table view, below the selected cell
// Update Table Data
myTableView.beginUpdates()
myTableView.insertRowsAtIndexPaths([
NSIndexPath(forRow: (indexPath.row)+1, inSection: 0)
], withRowAnimation: .Automatic)
let cell = myTableView.dequeueReusableCellWithIdentifier("DDOCell")
myTableView.endUpdates() */
//below reloads the table view after changes are made
myTableView.reloadData()
let user1 = SBDUserListQuery.init(userIds: ["2077103974"])
let user2 = SBDUserListQuery.init(userIds: ["2077103975"])
print("\n\n\n\n\n\n\n")
print(user1)
print(user2)
print("\n\n\n\n\n\n\n")
//below creates a channel between the two users
//needs to be replaced by a query for the user
SBDGroupChannel.createChannel(withUserIds: ["2077103974", "2077103975"], isDistinct: true, completionHandler: { (channel, error) in
if(error != nil) {
print("creation error!!!!")
} else {
//this means the channel was successfully created
print("Channel created")
//below sends a test message in the channel
channel!.sendUserMessage("Test Message", completionHandler: { (userMessage, error) in
if error != nil {
NSLog("Error: %@", error!)
return
} else {
print("Test message sent")
//also segue to the chatting view screen
let vc = self.storyboard!.instantiateViewController(withIdentifier: "ChattingView") as! Profile
self.present(vc, animated: true, completion: nil)
}
}) // end of sendUserMessage
}
}) //end of channelCreate
/*
* attach listener to text box, and show user typing(using the start typing method)
*/
/*
*Allow the receiving of messages
*/
}
}
下面是打印出的代码,以帮助调试过程。
User Logged In
test coiunt
0
The number of sections is: 0
test coiunt
0
The number of sections is: 0
test coiunt
0
The number of sections is: 0
test coiunt
0
The number of sections is: 0
Below is size of phoneNo
4
Below is name:
["Daniel Higgins", "Test2 Bot2", "John Appleseed", "John Appleseed"]
below is phone no
["5554787672", "5558881234", "8885551212", "8885555512"]
Successfully SendBird Login
此外,如果它有助于将tableView的当前dataSource和委托设置为View Controller,它将嵌入