我正在使用swifi创建类似SMS的应用程序。 我想显示发布到Firebase数据库的内容,但无法显示。 我是一个初学者,所以我认为小学部分是否有问题。 构建将成功。 即使您登录并显示主屏幕, 正在加载的动画继续旋转。
我要检索和显示的信息是名称,句柄,profilePic和两个文本数据。
编写HomeViewTableViewCell和HomeViewController的代码。 请。帮帮我...
HomeViewTableViewCellーーーーーーー的ーーーーーーーーー代码import UIKit
public class HomeViewTableViewCell: UITableViewCell {
@IBOutlet weak var profilePic: UIImageView!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var handle: UILabel!
@IBOutlet weak var whiteText: UITextView!
@IBOutlet weak var blackText: UITextView!
override public func awakeFromNib() {
super.awakeFromNib()
}
public func configure(name:String,handle:String,whiteText:String,blackText:String)
{
self.whiteText.text = whiteText
self.blackText.text = blackText
self.handle.text = "@"+handle
self.name.text = name
if((profilePic) != nil)
{
let imageData = NSData(contentsOf: NSURL(string:profilePic!)! as URL)
self.profilePic.image = UIImage(data:imageData! as Data)
}
else
{
self.profilePic.image = UIImage(named:"twitter")
}
}
}
HomeViewController的ーーーーーーーーーー代码
import UIKit
import FirebaseDatabase
import FirebaseAuth
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "Cell")
return cell
}
var databaseRef = Database.database().reference()
var loggedInUser:AnyObject?
var loggedInUserData:AnyObject?
@IBOutlet weak var aivLoading: UIActivityIndicatorView!
@IBOutlet weak var homeTableView: UITableView!
var whiteText = [AnyObject?]()
var blackText = [AnyObject?]()
override func viewDidLoad() {
super.viewDidLoad()
self.loggedInUser = Auth.auth().currentUser
self.databaseRef.child("whiteText/\(self.loggedInUser!.uid)").observe(.childAdded, with: { (snapshot:DataSnapshot) in
self.whiteText.append(snapshot)
})
self.databaseRef.child("blackText/\(self.loggedInUser!.uid)").observe(.childAdded, with: { (snapshot:DataSnapshot) in
self.blackText.append(snapshot)
self.homeTableView.insertRows(at: [IndexPath(row:0,section:0)], with: UITableViewRowAnimation.automatic)
self.aivLoading.stopAnimating()
}){(error) in
print(error.localizedDescription)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.whiteText.count
}
private func tableView(_ tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: HomeViewTableViewCell = tableView.dequeueReusableCell(withIdentifier: "HomeViewTableViewCell", for: indexPath as IndexPath) as! HomeViewTableViewCell
let submitW = (whiteText[(self.whiteText.count-1) - indexPath.row] as AnyObject).value(forKey: "text") as! String
let submitB = (blackText[(self.blackText.count-1) - indexPath.row] as AnyObject).value(forKey: "text") as!
cell.configure(name:self.loggedInUserData!.value(forKey: "name") as! String,handle:self.loggedInUserData!.value(forKey: "handle") as! String,whiteText:submitW,blackText:submitB)
return cell
}
}