简体   繁体   中英

My UITableView with custom xib cells disappear as soon as I start scrolling

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:[event] = []

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

       tableView.register(UINib(nibName: String(describing: CustomCell.self), bundle: nil), forCellReuseIdentifier: "customCell")

        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: "Spanish Workbook", eventLength: 3601, complete: false))
        ToDoViewController.ToDoEvents.append(event(eventName: "History Test", eventLength: 37, complete: false))
        ToDoViewController.ToDoEvents.append(event(eventName: "Lit Essay", eventLength: 40, complete: true))
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCell
        let currentEvent = ToDoViewController.ToDoEvents[indexPath.row]
        let eventName = currentEvent.name!
        let eventLength = currentEvent.time!
        let completion = currentEvent.isComplete!
        print(eventName)
        print(eventLength)
        print(completion)

        cell.customInit(name: "\(eventName)", time: "\(eventLength)", completed: completion)
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> 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.
    }
    */

}

This is my entire view controller class, but i am unable to find out why all of the data disappears as soon as I start scrolling down. The data is unable to be seen although you can see it initally. I cannot figure out why the data disappears and this the entire file. The custom init method has been shared below, although i believe that this is not the actual problem changing it.

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

    if(completed) {
        completedImage.image = UIImage(named: "C")
    } else {
        completedImage.image = UIImage(named: "R")
    }
    //self.contentView.backgroundColor = UIColor(red: 129/255.0, green: 25/255.0, blue: 207/255.0, alpha: 1)
}

import Foundation

class event{ var name: String? var time: Int? var isComplete: Bool?

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
}

}

you should change static number in numberOfRowsInSection

like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM