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.
}
*/
}
这是我的整个 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 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
}
}
Best Answer-推荐答案 strong>
您应该更改 numberOfRowsInSection 中的静态数字
喜欢:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ToDoViewController.ToDoEvents.count
}
关于ios - 一旦我开始滚动,我的带有自定义 xib 单元格的 UITableView 就会消失,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/53423690/
|