Xcode:在viewDidAppear

时间:2017-10-14 08:03:33

标签: ios arrays swift xcode uitableview

我现在很快就会检查我的代码8小时,我似乎无法找到错误。它工作得更早(我想我需要更好地使用GitHub)......

我正在Xcode中创建一个iOS应用程序,它从Firebase中提取数据,并且应该将每个条目显示为TableView中的新行。

它似乎正确地从数据库中提取数据,因此DataService代码似乎工作正常。问题似乎与feedVC.swift控制器有关。基本上显示了4行,但单元格中没有显示数据。当打印数组时,从ViewDidAppear()打印时它也是空的。当viewDidAppear()运行时,看起来数组还没有准备好......

希望有人可以提供帮助...对不起下面的长代码

模型 - > activity.swift

import Foundation

class Activity {
private var _userId: String
private var _name: String
private var _type: String
private var _weapon: String
private var _kills: String
private var _sightings: String
private var _note: String


var userId: String {
    return _userId
}

var name: String {
    return _name
}

var type: String {
    return _type
}

var weapon: String {
    return _weapon
}

var kills: String {
    return _kills
}

var sightings: String {
    return _sightings
}

var note: String {
    return _note
}




init(userId: String, name: String, type: String, weapon: String, kills: String, sightings: String, note: String) {
    self._userId = userId
    self._name = name
    self._type  = type
    self._weapon = weapon
    self._kills = kills
    self._sightings = sightings
    self._note = note

}

服务 - > DataService.swift

// this function uses the Model -> activity.swift model and pull all data for each activity in Firebase and append it to an array to be used by the app to generate the table view in FeedVC
func getAllFeedActivities(handler: @escaping (_ activities: [Activity]) -> ()) {
    var activityArray = [Activity]()
    REF_ACTIVITY.observeSingleEvent(of: .value) { (feedActivitySnapshop) in
        guard let feedActivitySnapshop = feedActivitySnapshop.children.allObjects as? [DataSnapshot] else { return }

    for activity in feedActivitySnapshop {
        let userId = activity.childSnapshot(forPath: "userId").value as! String // pull content from Firebase
        let name = activity.childSnapshot(forPath: "name").value as! String // pull content from Firebase
        let type = activity.childSnapshot(forPath: "type").value as! String // pull content from Firebase
        let weapon = activity.childSnapshot(forPath: "weapon").value as! String // pull content from Firebase
        let kills = activity.childSnapshot(forPath: "kills").value as! String // pull content from Firebase
        let sightings = activity.childSnapshot(forPath: "sightings").value as! String // pull content from Firebase
        let note = activity.childSnapshot(forPath: "note").value as! String // pull content from Firebase

        let activity = Activity(userId: userId, name: name, type: type, weapon: weapon, kills: kills, sightings: sightings, note: note)
        activityArray.append(activity)
       print("getAll")
       dump(activityArray)
    }

        handler(activityArray)
}
}

查看 - > feedCell.swift

import UIKit

class feedCell: UITableViewCell {

@IBOutlet weak var profileImage: UIImageView!
@IBOutlet weak var profileName: UILabel!
@IBOutlet weak var activityDate: UILabel!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var distance: UILabel!
@IBOutlet weak var sightings: UILabel!
@IBOutlet weak var kills: UILabel!

func configureCell(profileImage: UIImage, profileName: String, activityDate: String, name: String, distance: String, sightings: String, kills: String) {
    self.profileImage.image = profileImage
    self.profileName.text = profileName
    self.activityDate.text = activityDate
    self.name.text = name
    self.distance.text = distance
    self.sightings.text = sightings
    self.kills.text = kills
}

这一切都应该聚集在这里

控制器 - > feedVC.swift

import UIKit
import Firebase

class feedVC: UIViewController {

@IBOutlet weak var tableView: UITableView!

var activityArray = [Activity]()

override func viewDidLoad() {
    super.viewDidLoad()

    if Auth.auth().currentUser == nil {
        let authVC = self.storyboard?.instantiateViewController(withIdentifier: "authVC") as? authVC
        self.present(authVC!, animated: true, completion: nil)
    }

    tableView.delegate = self
    tableView.dataSource = self



}


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    DataService.instance.getAllFeedActivities { (returnedActivityArray) in
        self.activityArray = returnedActivityArray
        self.tableView.reloadData()

    }
    print("test")
    dump(activityArray)
}


// Footer menu

@IBAction func clubBtnWasPressed(_ sender: Any) {
}

@IBAction func recordBtnWasPressed(_ sender: Any) {
    self.performSegue(withIdentifier: "feedToRecordVC", sender: self)

}

@IBAction func ProfileBtnWasPressed(_ sender: Any) {
        self.performSegue(withIdentifier: "feedToProfileVC", sender: self)
}

@IBAction func MoreBtnWasPressed(_ sender: Any) {
}


}


extension feedVC: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return activityArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell") as? feedCell else
    {return UITableViewCell() }
    let image = UIImage(named: "profile_image")
    let activity = activityArray[indexPath.row]
    cell.configureCell(profileImage: image!, profileName: "Test name", activityDate: "8 October", name: activity.name, distance: "8 km", sightings: activity.sightings, kills: activity.kills)
    return cell
}
}

来自控制台的输出:

  

2017-10-14 09:56:40.300359 + 0200 Shoota MapKit [7409:1907989]   [BoringSSL]函数boringssl_context_get_peer_sct_list:第1754行   收到的sct扩展长度小于sct数据长度测试    - 0个元素2017-10-14 09:56:40.619815 + 0200 Shoota MapKit [7409:1907974] TIC阅读状态[1:0x0]:1:57 2017-10-14   09:56:40.620042 + 0200 Shoota MapKit [7409:1907974] TIC阅读状态   [1:0x0]:1:57getAll▿1元素▿Shoota_MapKit.Activity#0        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“11”        - _type:“22”        - _weapon:“33”        - _kills:“44”        - _sightings:“55”        - _note:“66”getAll▿2个元素▿Shoota_MapKit.Activity#0        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“11”        - _type:“22”        - _weapon:“33”        - _kills:“44”        - _sightings:“55”        - _note:“66”▿Shoota_MapKit.Activity#1        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“AA”        - _type:“BB”        - _weapon:“CC”        - _kills:“DD”        - _sightings:“FF”        - _note:“GG”getAll▿3个元素▿Shoota_MapKit.Activity#0        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“11”        - _type:“22”        - _weapon:“33”        - _kills:“44”        - _sightings:“55”        - _note:“66”▿Shoota_MapKit.Activity#1        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“AA”        - _type:“BB”        - _weapon:“CC”        - _kills:“DD”        - _sightings:“FF”        - _note:“GG”▿Shoota_MapKit.Activity#2        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“晚上狩猎”        - _type:“Birds”        - _weapon:“Browning CAL.12”        - _kills:“3”        - _sightings:“20”        - _note:“狩猎松鸡的好日子”getAll▿4个元素▿Shoota_MapKit.Activity#0        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“11”        - _type:“22”        - _weapon:“33”        - _kills:“44”        - _sightings:“55”        - _note:“66”▿Shoota_MapKit.Activity#1        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“AA”        - _type:“BB”        - _weapon:“CC”        - _kills:“DD”        - _sightings:“FF”        - _note:“GG”▿Shoota_MapKit.Activity#2        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“晚上狩猎”        - _type:“Birds”        - _weapon:“Browning CAL.12”        - _kills:“3”        - _sightings:“20”        - _note:“狩猎松鸡的好日子”▿Shoota_MapKit.Activity#3        - _userId:“NhZZGwJQCGe2OGaNTwGvpPuQKNA2”        - _name:“asdf”        - _type:“fdgjj”        - _weapon:“gccv”        - _kills:“bcc”        - _sightings:“Gucci”        - _note:“vvbb”

UI的屏幕截图(feedVC.swift) screenshot of UI

0 个答案:

没有答案