以编程方式添加 TableView 时未调用 CellForRowAt

如何解决以编程方式添加 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
}
}

我得到的输出是这样的:

Section 4.1.4

----- 编辑------
根据我在评论部分收到的不同建议,我稍微修改了我的代码,将 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
    }
 }

我真正得到的输出是这个

enter image description here

导致 TableView 行为的原因可能是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com(将#修改为@)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?