我目前正在使用firebase数据库中的内容填充tableView。我能够打印出要在单元格中填充的特定信息,并且通过滚动适当长度的填充数据来指示物理上正在填充的单元格。我不确定为什么它会访问信息并填充单元格,而不是将数据插入单元格?
TableView:
import UIKit
import FirebaseAuth
import FirebaseStorage
class Articles: UITableViewController {
var vcType:String = "Home"
//var valueTopass = [[String:String]]()
var rooms = [Room]()
var articleCell = ArticlesCell()
@IBOutlet weak var menuButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
if self.vcType == "Home"
{
self.rooms += ArticlesManager.sharedClient.rooms
}
else
{
if let obj = ArticlesManager.sharedClient.catRooms[self.vcType.lowercased()] //as? [Room]
{
self.rooms += obj
}
}
self.tableView.reloadData()
ArticlesManager.sharedClient.blockValueChangeInRoomArray = {
newRoom in
if self.vcType == "Home"
{
self.rooms.append(newRoom)
self.rooms.sort(by: {
if $0.created_Date == nil
{
return false
}
if $1.created_Date == nil
{
return true
}
return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending
})
}
else
{
if self.vcType.lowercased() == newRoom.category
{
self.rooms.append(newRoom)
self.rooms.sort(by: {
if $0.created_Date == nil
{
return false
}
if $1.created_Date == nil
{
return true
}
return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending
})
self.tableView.reloadData()
}
}
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rooms.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (indexPath as NSIndexPath).row == 0 {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "featured", for: indexPath) as! featuredCell
let room = rooms[(indexPath as NSIndexPath).row]
cell2.configureCell(room)
self.tableView.reloadData()
return cell2
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ArticlesCell
let room = rooms[(indexPath as NSIndexPath).row]
cell.configureCell(room)
self.tableView.reloadData()
return cell
}
}
}
ArticleManager:
import UIKit
class ArticlesManager: NSObject {
static let sharedClient = ArticlesManager()
var dateFormater:DateFormatter{
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss a z"
return df
}
var roomsitems = ["Home", "News", "Features", "Opinion", "Sports", "Entertainment", "Editor"]
var rooms = [Room]()
var catRooms = [String:[Room]]()
var blockValueChangeInRoomArray:((Room) -> ())!
func fetchData()
{
Data.dataService.fetchData {
(room) in
self.rooms.append(room)
self.rooms.sort(by: {
if $0.created_Date == nil
{
return false
}
if $1.created_Date == nil
{
return true
}
return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending
})
if let obj = self.catRooms[room.category] //as? [Room]
{
var objRooms = obj
objRooms.append(room)
objRooms.sort(by: {
if $0.created_Date == nil
{
return false
}
if $1.created_Date == nil
{
return true
}
return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending })
self.catRooms[room.category] = objRooms
}
else
{
self.catRooms[room.category] = [room]
}
if self.blockValueChangeInRoomArray != nil
{
self.blockValueChangeInRoomArray(room)
}
}
}
}
DataService:
import UIKit
class ArticlesManager: NSObject {
static let sharedClient = ArticlesManager()
var dateFormater:DateFormatter{
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss a z"
return df
}
var roomsitems = ["Home", "News", "Features", "Opinion", "Sports", "Entertainment", "Editor"]
var rooms = [Room]()
var catRooms = [String:[Room]]()
var blockValueChangeInRoomArray:((Room) -> ())!
func fetchData()
{
Data.dataService.fetchData {
(room) in
self.rooms.append(room)
self.rooms.sort(by: {
if $0.created_Date == nil
{
return false
}
if $1.created_Date == nil
{
return true
}
return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending
})
if let obj = self.catRooms[room.category] //as? [Room]
{
var objRooms = obj
objRooms.append(room)
objRooms.sort(by: {
if $0.created_Date == nil
{
return false
}
if $1.created_Date == nil
{
return true
}
return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending })
self.catRooms[room.category] = objRooms
}
else
{
self.catRooms[room.category] = [room]
}
if self.blockValueChangeInRoomArray != nil
{
self.blockValueChangeInRoomArray(room)
}
}
}
}
答案 0 :(得分:3)
从cellForRowAt indexPath
方法中删除此行代码:
self.tableView.reloadData()
这是连续重新加载表而没有机会 configureCell 和显示数据。
答案 1 :(得分:0)
我能够弄清楚我遇到的具体问题。当我使用firebase时,关于谁可以阅读"的规则默认情况下,设置为经过身份验证的用户。要更改此设置,您必须进入firebase控制台并在加载表视图之前将其更改为公用或验证用户。无论哪种方式,我希望这能够帮助你!