我有一个表格视图,可在应用启动时正常运行。当我切换应用程序语言并点击日历文本时,它会调用委托方法来过滤数据,设置标志并重新加载表格视图。在表视图委托方法中,我检查标志并从适当的数组中获取数据。语言切换后,该数组返回1,但是没有调用cellforRowAt indexPath
方法。
extension CalendarViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (isFilterActive) {
return filteredData.count // returns 1 but cellForRowAt is not getting called
}
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell") as! CalendarCell
cell.delegate = self
// ...
}
}
extension CalendarViewController: CalendarViewDelegate {
func filterEvent(for date: Date) {
isFilterActive = true
var i = 0
filteredData = []
filteredImagesData = [:]
for (idx, val) in data.enumerated() {
var fromDate: Date?
var toDate: Date?
// ..
filteredData.append(val)
i = i + 1
}
}
DispatchQueue.main.async {
self.eventTableView.reloadData() // <-- filters and calls reload
}
}
}
DateViewControllerClass which displays
JTAppleCalendarView`是
class DateViewController: UIViewController {
let delegate = JSDelegate.shared
weak var calendarVCDelegate: CalendarViewDelegate?
@IBOutlet weak var calendarView: JTAppleCalendarView!
override func viewDidLoad() {
calendarVCDelegate = self.delegate.getCalendarViewDelegate()
// ...
}
// ...
}
extension DateViewController: JTAppleCalendarViewDelegate, JTAppleCalendarViewDataSource {
func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
guard let dateCell = cell as? DateCell else { return }
if (cellState.dateBelongsTo == .thisMonth) {
dateCell.dateLabel.textColor = UIColor.white
dateCell.selectedView.isHidden = false
}
self.calendarVCDelegate!.filterEvent(for: date) // <- calls delegate method on date cell tap
}
}
我在情节提要中连接了delegate
和datasource
。我还有其他过滤器,如果在切换语言之前确实选择了它们,则只有重新加载表才有效。该如何解决?
答案 0 :(得分:0)
看起来好像有一个不同的对象用于tableview左右。不太清楚。我通过使用以下方法解决了它。
我没有在扩展方法中调用self.eventTableView.reloadData()
,而是发布了通知NotificationCenter.default.post(name: Constants.AppNotification.EventTableViewShouldReload, object: self)
。通过以下方法将观察者作为viewDidLoad()
添加到NotificationCenter.default.addObserver(forName: Constants.AppNotification.EventTableViewShouldReload, object: nil, queue: nil, using: eventTableViewShouldReload)
中:
@objc func eventTableViewShouldReload(notif: Notification) {
print("reload data notif \(CalendarModel.filteredData.count)")
CalendarModel.isFilterActive = true
self.eventTableView.reloadData()
}
我创建了一个数据模型类来保存静态变量,没有这些变量,这些变量将被清除。
struct CalendarModel {
static var data:[[String:Any]] = []
static var filteredData:[[String:Any]] = []
static var imagesData:[Int:UIImage] = [:]
static var filteredImagesData:[Int:UIImage] = [:]
static var isFilterActive = false
}
现在,当重新加载表视图时,它可以正常工作。没有CalendarModel
静态变量,我得到以下日志
reload data notif 1
numberOfRowsInSection 1
reload data notif 0 <-- called twice
numberOfRowsInSection 0 <-- second one, the filteredData.count is getting 0
cellForRowAt
configureCell