所有UITableCells在UITableView中滚动并触摸时消失。 一切都是以编程方式完成的,我使用标准的UITableViewCell类。
我在某处读到包含我的单元格数据的数组可能被清空,因此视图无法在滚动时加载数据。但是,我不知道如何验证这一点。
这是我的TableView委托和DataSource:
curl -si https://httpbin.org/put -X PUT -d '{"text": "hello"}' > test1
curl -si https://httpbin.org/put -X PUT -d '{"text": "hello"}' \
-H 'Content-Type: application/x-www-form-urlencoded' > test2
diff test1 test2 # Only the dates and processing times differ
在我的ViewController的ViewDidLoad方法中,我正在执行以下操作:
class LessonTable: NSObject, UITableViewDataSource, UITableViewDelegate {
private var lessons = [LessonItem]()
public var backgroundColor = UIColor(white: 0.97, alpha: 1)
func addLessons(_ lessons: [LessonItem], tableView: UITableView){
tableView.beginUpdates()
//Construct array of indexPaths
var indexPaths = [IndexPath]()
for i in 0...lessons.count {
indexPaths.append(IndexPath(row: lessons.count - 1 + i, section: 0))
}
//Append all lessons to Lessons array
self.lessons.append(contentsOf: lessons)
//Insert rows
tableView.insertRows(at: indexPaths, with: .automatic)
tableView.endUpdates()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return lessons.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "lessonCell", for: indexPath)
cell.backgroundColor = backgroundColor
let row = indexPath.row
cell.textLabel?.text = lessons[row].Title
cell.detailTextLabel?.text = lessons[row].Start.Formatted() + " - " + lessons[row].Stop.Formatted()
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let row = indexPath.row
print(lessons[row].Title)
}
}
还
let upcomingLessonsTableView = UITableView(frame: CGRect(x: 0, y: 100, width: (card.frame.size.width), height: 90))
let upcomingLessonsTableViewData = LessonTable()
upcomingLessonsTableView.dataSource = upcomingLessonsTableViewData
upcomingLessonsTableView.delegate = upcomingLessonsTableViewData
upcomingLessonsTableView.register(UITableViewCell.self, forCellReuseIdentifier: "lessonCell")
upcomingLessonsTableView.backgroundColor = upcomingLessonsTableViewData.BackgroundColor
card.addSubview(upcomingLessonsTableView)
其中upcomingLessonsTableViewData.addLessons(upcomingEvents, tableView: upcomingLessonsTableView)
是一个数组。
新发现。 设置连续打印出课程数组内容的计时器时,单元格不会消失。为什么?毫无头绪。
答案 0 :(得分:1)
将以下变量设为全局变量。 let comingLessonsTableViewData;
答案 1 :(得分:0)
在cellForRow
方法中添加as! LessonCell
您的let cell
行
应该像:
let cell = tableView.dequeueReusableCell(withIdentifier: "lessonCell", for: indexPath) as! LessonCell