我开始滚动后,带有自定义Xib单元格的UITableView消失了

时间:2018-11-22 04:00:40

标签: ios swift

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.
    }
    */

}

这是我的整个视图控制器类,但是当我开始向下滚动时,我无法找出所有数据为何消失的原因。尽管您可以初始看到数据,但无法看到数据。我无法弄清楚为什么数据消失了,而整个文件消失了。自定义init方法已在下面共享,尽管我认为这并不是更改它的实际问题。

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)
}

导入基金会

班级活动{     var名称:字符串?     var time:整数?     var isComplete:布尔?

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
}

}

1 个答案:

答案 0 :(得分:0)

您应该在numberOfRowsInSection中更改静态数字

like:

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