gpt4 book ai didi

swift - HeaderCell 的 UITableView 性能问题

转载 作者:行者123 更新时间:2023-11-28 14:14:32 25 4
gpt4 key购买 nike

当 HeaderCell 变得可见时,我遇到了性能问题,它在 iPhone X 上占用了 40% 的 CPU,而且我看到了延迟。这个 UITableView 显示静态数据(电视指南)。每次上下滚动时,我都会看到延迟,“tableView.dequeueReusableCell()”无济于事。如果我使用我的 ExpandableHeaderView() 创建 HeaderCell 以支持点击和扩展部分,我会遇到同样的问题。如果展开部分并在元素内部滚动,我看不到滞后。只有当 HeaderCell 出现在屏幕上时才会出现性能问题。当用户打开 View Controller 时,他会看到每个 channel 的 3 个节目(当前和接下来的两个),如果他点击部分名称,他会看到部分( channel )的所有节目。 HeaderCell 仅包含标签。我在我的 Android 应用程序上以相同的方式显示相同的数据,它在简单的 Android 设备上运行没有任何问题。

func numberOfSections(in tableView: UITableView) -> Int {
return Channels.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Channels[section].Programs.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)! as! TVGuideTableViewCell
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let text = Channels[indexPath.section].Programs[indexPath.row].Name
let dateString = dateFormatter.string(from:Channels[indexPath.section].Programs[indexPath.row].StartDate!)
cell.label.text = text
cell.timeLabel.text = dateString

if (Channels[indexPath.section].Programs[indexPath.row].EndDate! < currentDate) {
cell.label.textColor = UIColor.gray
} else {
cell.label.textColor = UIColor.white
}

if (Channels[indexPath.section].Programs[indexPath.row].StartDate! < currentDate) {
cell.bell.isHidden = true
} else {
cell.bell.isHidden = false
}

if (Channels[indexPath.section].Programs[indexPath.row].EndDate! < currentDate) {
Channels[indexPath.section].Programs[indexPath.row].expandedRow = false
} else {
if Channels[indexPath.section].expandedCount < 3 {
Channels[indexPath.section].expandedCount += 1
Channels[indexPath.section].Programs[indexPath.row].expandedRow = true
}
}

return cell
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifierHeader) as! CustomHeaderTableViewCell
headerCell.headerLabel.text = Channels[section].Name
return headerCell
// let header = ExpandableHeaderView()
// header.customInit(title: Channels[section].Name, section: section, delegate: self as ExpandableHeaderViewDelegate)
// return header
}

func toggleSection(header: ExpandableHeaderView, section: Int) {
Channels[section].expanded = !Channels[section].expanded
Channels[section].expandedCount = 0
tableView.beginUpdates()
for i in 0 ..< Channels[section].Programs.count {
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
tableView.endUpdates()
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 38
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(Channels[indexPath.section].expanded) {
return 38
} else {

if (Channels[indexPath.section].Programs[indexPath.row].EndDate! < currentDate) {
return 0
} else {
if Channels[indexPath.section].Programs[indexPath.row].expandedRow {
return 38.0
} else {
return 0.0
}
}

}
}

class CustomHeaderTableViewCell: UITableViewCell {


@IBOutlet weak var headerLabel: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}

}

最佳答案

从您的代码中,我可以看出问题不在于HeaderCell,而在于DateFormatter 对象。因为每次滚动和单元格出现时,都会创建一个新对象,想象一下,如果一次有 10 - 15 个单元格,然后再使用/创建 10 - 15 个单元格,当你滚动或重新加载数据时,它会再次创建一个新对象。

所以,我的建议是在 cellforRow 之外创建 DateFormatter 对象,然后通过调用它来获取 dateString .

这只是一个假设,试试这个,项目仍然出现,请告诉我!

关于swift - HeaderCell 的 UITableView 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52244944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com