gpt4 book ai didi

ios - 在开始直播后获取 UITableView 来刷新用户

转载 作者:行者123 更新时间:2023-11-30 12:39:05 25 4
gpt4 key购买 nike

我正在开发的应用程序的登陆页面有一个以应用程序命名的占位符头像,当没有用户主动进行实时流式传输时(使用 Red5 Pro 流式传输框架),该应用程序会出现该占位符头像。

但是,当有人开始开始直播时,我希望它自动刷新表格 View 并显示新用户的头像。到目前为止我所写的内容是有效的,但并不完全有效。当有人开始直播时,占位头像确实消失,但正在直播的用户的头像不会出现。

如果我关闭应用程序并重新打开它,它就会正确显示。这是我在 Swift 3 中的代码,我做错了什么?是否应该将刷新调用移出 ViewDidLoad?我是否错误地使用了调度队列?谢谢

import UIKit
import Firebase

class HomeController: UIViewController, UITableViewDataSource, UITableViewDelegate, cellDelegate {

@IBOutlet var tableView: UITableView!
var stream: String!
var top: [SSStream] = []
var recent: [SSStream] = [SSStream()]
var trending: [SSStream] = [SSStream()]
var ref: FIRDatabaseReference!

override func viewDidLoad() {
navigationItem.title = "Swiffshot"
navigationController?.navigationBar.isTranslucent = false
let settings = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
settings.setImage(#imageLiteral(resourceName: "Settings"), for: .normal)
settings.addTarget(self, action: #selector(settingsPressed), for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: settings)
let friends = UIButton(frame: CGRect(x: 0, y: 0, width: 23, height: 20))
friends.setImage(#imageLiteral(resourceName: "AllFriends"), for: .normal)
friends.addTarget(self, action: #selector(friendsPressed), for: .touchUpInside)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: friends)

let nib = UINib(nibName: "MainHeader", bundle: Bundle.main)
tableView.register(nib, forHeaderFooterViewReuseIdentifier: "MainHeader")

ref = FIRDatabase.database().reference()


if !SSContact.shared.active {
performSegue(withIdentifier: "fromMainToAuth", sender: self)
}

SSContact.shared.load() { SSContact.shared.propertyCheck(self) { } }

// SSContact.shared.subscribeToTop(pulse: { (streams) in
// self.top.removeAll()
// self.top.append(contentsOf: streams)
// self.tableView.reloadSections(IndexSet(integer: 0), with: .automatic)
// })

ref.child("streams").observe(.value, with: { (snapshot) in

print("I ran")

self.top.removeAll()
if let userData = snapshot.value as? NSDictionary {
for stream in userData {
let newStream = SSStream()
newStream.username = stream.key as! String
print("Found stream \(stream.key as! String)")
newStream.isPrivate = !((stream.value as! NSDictionary)["public"] as! Bool)
newStream.views = (stream.value as! NSDictionary)["views"] as! Int
newStream.isEnabled = true
self.top.append(newStream)
}
}
if self.top.isEmpty {
print("No Streams Found")
self.top.append(SSStream())
}
DispatchQueue.main.async {
self.tableView.reloadSections(IndexSet(integer: 0), with: .automatic)
self.tableView.reloadData()
}
})
}

func cellGotPressed(_ stream: String) {
self.stream = stream
performSegue(withIdentifier: "toPlayer", sender: self)
}

func settingsPressed() {
performSegue(withIdentifier: "toSettings", sender: self)
}

func friendsPressed() {
performSegue(withIdentifier: "fromMainToExpandable", sender: self)
}

func cameraTapped() {
performSegue(withIdentifier: "toRed", sender: self)
}

func cellTapped() {
print("Cell Tapped")
}

// MARK: Segue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPlayer" {
let player = segue.destination as! VideoPlayerViewController
player.isSubscribing = true
player.stream = stream
}
}

// MARK: Table View Functions

func numberOfSections(in tableView: UITableView) -> Int {
return 3
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "big") as! CategoryRow
cell.section = indexPath.section
cell.top = top
cell.delegate = self
cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(cameraTapped)))
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "small") as! CategoryRow
cell.section = indexPath.section
cell.recent = recent
cell.trending = trending
cell.delegate = self
return cell
}
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
return nil
} else {
let cell = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: "MainHeader")
let header = cell as! MainHeader
if section == 1 {
header.fillHeader("RECENT")
} else if section == 2 {
header.fillHeader("Trending + Now")
} else {
print("Unknown Section")
}
return header
}
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 300
} else {
return 100
}
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 0
} else {
return 50
}
}

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 50
}
}

最佳答案

我意识到我需要做什么。我需要设置

ref.child("streams").observe

在对 Dispatch.Queue 的调用中。通过在调用调度之前设置引用,程序无法正确同步。应该是这样的:

DispatchQueue.main.async {
ref.child("streams").observe(.value, with: { (snapshot) in
self.tableView.reloadSections(IndexSet(integer: 0), with: .automatic)
self.tableView.reloadData()
}
})

关于ios - 在开始直播后获取 UITableView 来刷新用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42471560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com