如何解决以编程方式添加 TableView 时未调用 CellForRowAt
我正在创建一个 UI(以编程方式),它将显示两个主要部分
-
第一部分将显示一个视频播放器
-
第二部分将展示一个包含更多视频的 UITableView
我已经成功地完成了第一部分,它正在运行,并且占据了屏幕顶部的一小部分
使用相同的逻辑,我尝试在主容器视图中添加一个 UITableView,但我只能看到一个空的 tableView
我还尝试在调用 cellForRowAt 时记录消息,但没有打印任何内容,我得出的结论是没有调用 cellForRowAt
这是完成所有这些工作的类的代码:
import UIKit
import AVFoundation
class VideoLauncher: NSObject,UITableViewDataSource,UITableViewDelegate{
let data : [String] = ["String 1","String 2","String 3","String 4","String 4"]
// TableView initialization
let tableView : UITableView = {
let t = UITableView()
t.translatesAutoresizingMaskIntoConstraints = false
t.register(UITableViewCell.self,forCellReuseIdentifier: "customCell")
t.rowHeight = 80
return t
}()
func showVideoPlayer(){
if let keyWindow = UIApplication.shared.keyWindow {
// View Container that will contain the player and the tableview
let mainContainer = UIView(frame: keyWindow.frame)
mainContainer.backgroundColor = .white
mainContainer.frame = CGRect(x: keyWindow.frame.width - 30,y: keyWindow.frame.height - 30,width: 10,height: 10)
// Creating and adding the first part : PlayerView
let height = keyWindow.frame.width * 9 / 16
let videoPlayerFrame = CGRect(x: 0,y: 0,width: keyWindow.frame.width,height: height)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
mainContainer.addSubview(videoPlayerView)
// Creating and adding the second Part: TableView
// I'm adding the tableView to another UIView : tableViewContainer
let tableViewContainer = UIView()
tableViewContainer.frame = CGRect(x: 0,y: videoPlayerView.frame.maxY,height: keyWindow.frame.height)
mainContainer.addSubview(tableViewContainer)
tableView.delegate = self
tableView.dataSource = self
tableView.frame = CGRect(x: 0,width: tableViewContainer.frame.width,height: tableViewContainer.frame.height)
tableViewContainer.addSubview(tableView)
keyWindow.addSubview(mainContainer)
UIView.animate(withDuration: 0.5,delay: 0,usingSpringWithDamping: 1,initialSpringVelocity: 1,options: .curveEaseOut) {
mainContainer.frame = CGRect(x: keyWindow.frame.minX,y: keyWindow.frame.minY + 35,height: keyWindow.frame.height)
} completion: { (Bool) in
}
}
}
// MARK: - TABLEVIEW DATA SOURCE
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
print("NumberOfRows In Section")
return data.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("Cell For row At")
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell",for: indexPath)
let s = data[indexPath.row]
cell.textLabel?.text = s
return cell
}
}
我得到的输出是这样的:
----- 编辑------
根据我在评论部分收到的不同建议,我稍微修改了我的代码,将 tableView 锚定到其容器并在动画完成时调用 tableView.reloadData()。
因此,CellForRowAt 被调用一次,并且单元格显示但消失的速度与它们出现的速度一样快。
下面是我之前代码的修改版
class VideoLauncher: NSObject,UITableViewDataSource{
let data : [String] = ["String 1","String 1","String 4"]
// TableView initialization
let tableView : UITableView = {
let t = UITableView()
t.register(UITableViewCell.self,forCellReuseIdentifier: "customCell")
return t
}()
func showVideoPlayer(){
self.tableView.dataSource = self
if let keyWindow = UIApplication.shared.keyWindow {
// View Container that will contain the player and the tableview
let mainContainer = UIView(frame: keyWindow.frame)
mainContainer.backgroundColor = .white
mainContainer.frame = CGRect(x: keyWindow.frame.width - 30,height: 10)
// Creating and adding the first part : PlayerView
let height = keyWindow.frame.width * 9 / 16
let videoPlayerFrame = CGRect(x: 0,height: height)
let videoPlayerView = VideoPlayerView(frame: videoPlayerFrame)
mainContainer.addSubview(videoPlayerView)
// Creating and adding the second Part: TableView
// I'm adding the tableView to another UIView : customView
let tableViewContainer = UIView()
tableViewContainer.translatesAutoresizingMaskIntoConstraints = false
mainContainer.addSubview(tableViewContainer)
tableViewContainer.topAnchor.constraint(equalTo: videoPlayerView.bottomAnchor).isActive = true
tableViewContainer.bottomAnchor.constraint(equalTo: mainContainer.bottomAnchor).isActive = true
tableViewContainer.leftAnchor.constraint(equalTo: mainContainer.leftAnchor).isActive = true
tableViewContainer.rightAnchor.constraint(equalTo: mainContainer.rightAnchor).isActive = true
tableViewContainer.addSubview(self.tableView)
self.tableView.translatesAutoresizingMaskIntoConstraints = false
self.tableView.topAnchor.constraint(equalTo: tableViewContainer.topAnchor).isActive = true
self.tableView.bottomAnchor.constraint(equalTo: tableViewContainer.bottomAnchor).isActive = true
self.tableView.leftAnchor.constraint(equalTo: tableViewContainer.leftAnchor).isActive = true
self.tableView.rightAnchor.constraint(equalTo: tableViewContainer.rightAnchor).isActive = true
keyWindow.addSubview(mainContainer)
UIView.animate(
withDuration: 0.5,options: .curveEaseOut) {
mainContainer.frame = CGRect(x: keyWindow.frame.minX,height: keyWindow.frame.height)
} completion: { (Bool) in
self.tableView.reloadData()
}
}
}
// MARK: - TABLEVIEW DATA SOURCE
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
print("NumberOfRows In Section")
return data.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("Cell For row At")
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell",for: indexPath)
let s = data[indexPath.row]
cell.textLabel?.text = s
return cell
}
}
我真正得到的输出是这个
导致 TableView 行为的原因可能是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com(将#修改为@)