我正在创建一个活动应用,其中UITableView
中列出了数千名参与者。该过程应该是checkIn
(通过点击按钮)2个或更多参与者,并拉动刷新。 “拉入刷新”成功执行,但是当我尝试拉入/向上滚动数据时,它崩溃并显示Thread 1: Fatal error: Index out of range
。希望您能有所帮助,因为我尝试在SO中使用解决方案,但仍无法正常工作。谢谢。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return cellHeights[indexPath.row]
}
UIRefreshControl代码
var refresher: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
createCellHeightsArray()
refresher = UIRefreshControl()
refresher.attributedTitle = NSAttributedString(string: "Pull to refresh")
refresher.addTarget(self, action: #selector(ParticipantsViewController.refresh), for: UIControlEvents.valueChanged)
ParticipantTableView.addSubview(refresher)
countNotif()
getData()
}
@objc func refresh() {
countNotif()
getParticipants()
refresher.endRefreshing()
ParticipantTableView.reloadData()
}
func getData() {
getParticipants()
if let posts = participants {
self.participants = posts
} else {
self.participants.removeAll()
}
self.refresh()
}
cellHeightArray
func createCellHeightsArray() {
cellHeights.removeAll()
if searchController.isActive && searchController.searchBar.text != "" {
for _ in 0...filteredParticipants.count {
cellHeights.append(kCloseCellHeight)
}
}else {
for _ in 0...participants.count {
cellHeights.append(kCloseCellHeight)
}
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard case let cell as ParticipantCell = cell else {
return
}
cell.backgroundColor = UIColor.clear
if searchController.isActive && searchController.searchBar.text != "" {
cell.participant = filteredParticipants[indexPath.row]
}else {
cell.participant = participants[indexPath.row]
}
if cellHeights[(indexPath as NSIndexPath).row] == kCloseCellHeight {
cell.unfold(false, animated: false, completion: nil)
} else {
cell.unfold(true, animated: false, completion: nil)
}
}
答案 0 :(得分:0)
这应该有帮助
filteredParticipants.count-1
或0..<unsortedStrings.count
0...participants.count-1
或0..<participants.count
func createCellHeightsArray() {
cellHeights.removeAll()
if searchController.isActive && searchController.searchBar.text != "" {
for _ in 0..<filteredParticipants.count {
cellHeights.append(kCloseCellHeight)
}
}else {
for _ in 0..<participants.count {
cellHeights.append(kCloseCellHeight)
}
}
}