菜鸟教程小白 发表于 2022-12-11 20:51:21

ios - 一旦我开始滚动,我的带有自定义 xib 单元格的 UITableView 就会消失


                                            <p><pre><code>import UIKit

class ToDoViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var DateLabel: UILabel!
    @IBOutlet weak var MonthLabel: UILabel!
    @IBOutlet weak var DayLabel: UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var newButton: UIImageView!

    static var ToDoEvents: = []

    override func viewDidLoad() {
      super.viewDidLoad()
      ToDoViewController.readFromFile()
       // let nib = UINib(nibName: &#34;CustomCell&#34;, bundle: nil)
       // tableView.register(nib, forCellReuseIdentifier: &#34;customCell&#34;)
      //let nib = UINib.init(nibName: &#34;customCell&#34;, bundle: nil)
       // tableView.register(nib, forCellReuseIdentifier: &#34;customCell&#34;)

       tableView.register(UINib(nibName: String(describing: CustomCell.self), bundle: nil), forCellReuseIdentifier: &#34;customCell&#34;)

      self.tableView.delegate = self
      self.tableView.dataSource = self

      // Do any additional setup after loading the view.
    }

    public static func readFromFile(){
      ToDoViewController.ToDoEvents.append(event(eventName: &#34;Spanish Workbook&#34;, eventLength: 3601, complete: false))
      ToDoViewController.ToDoEvents.append(event(eventName: &#34;History Test&#34;, eventLength: 37, complete: false))
      ToDoViewController.ToDoEvents.append(event(eventName: &#34;Lit Essay&#34;, eventLength: 40, complete: true))
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return ToDoViewController.ToDoEvents.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: &#34;customCell&#34;) as! CustomCell
      let currentEvent = ToDoViewController.ToDoEvents
      let eventName = currentEvent.name!
      let eventLength = currentEvent.time!
      let completion = currentEvent.isComplete!
      print(eventName)
      print(eventLength)
      print(completion)

      cell.customInit(name: &#34;\(eventName)&#34;, time: &#34;\(eventLength)&#34;, completed: completion)
      return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      return 100
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      // Get the new view controller using segue.destination.
      // Pass the selected object to the new view controller.
    }
    */

}
</code></pre>

<p>这是我的整个 ViewController 类,但我无法找出为什么一旦我开始向下滚动所有数据就会消失。尽管您最初可以看到数据,但无法看到数据。我无法弄清楚为什么数据会消失以及整个文件消失的原因。下面分享了自定义的 init 方法,虽然我相信这不是改变它的实际问题。</p>

<pre><code>func customInit(name: String, time: String, completed: Bool){
    self.eventName.text = name
    self.lengthLabel.text = time

    if(completed) {
      completedImage.image = UIImage(named: &#34;C&#34;)
    } else {
      completedImage.image = UIImage(named: &#34;R&#34;)
    }
    //self.contentView.backgroundColor = UIColor(red: 129/255.0, green: 25/255.0, blue: 207/255.0, alpha: 1)
}
</code></pre>

<p>导入基础</p>

<p>类事件{
    变量名称:字符串?
    变量时间:整数?
    var isComplete:bool 值?</p>

<pre><code>init(eventName: String, eventLength: Int) {
    self.name = eventName
    self.time = eventLength
    self.isComplete = false
}

init(eventName: String, eventLength: Int, complete: Bool) {
    self.name = eventName
    self.time = eventLength
    self.isComplete = complete
}
</code></pre>

<p>}</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该更改 numberOfRowsInSection 中的静态数字</p>

<p>喜欢:</p>

<pre><code>func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return ToDoViewController.ToDoEvents.count
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 一旦我开始滚动,我的带有自定义 xib 单元格的 UITableView 就会消失,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/53423690/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/53423690/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 一旦我开始滚动,我的带有自定义 xib 单元格的 UITableView 就会消失