ios - 奇怪的行为设置 tableView 行高和 scrollPosition Swift
问题描述
同样,viewController
我有一个calendarTableView
代表月份的日子。在cellForRowAt
我选择与当天对应的行并调用一个函数来填充timeSlotCollectionView
。一切正常,但我有一点奇怪的情况。
案例 1:行height
= 自动和scrollPosition.none
=timeSlotCollectionView
不跟随cornerRadius
案例 2:行height
= 自动和= 线程 1:单元格定义scrollPosition.middle
上的 EXC_BAD_ACCESS(代码 = 2,地址 = 0x659ef4)calendarTableView
Case3:Row height
= 44 和 scrollPosition.none =calendarTableView
单元格不跟随cornerRadius
案例 4:行height
= 44 和scrollPosition.middle
=calendarTableView
单元格不跟随cornerRadius
案例 5:行heigh
t = 60 并且scrollPosition.none
=calendarTableView
单元格确实遵循cornerRadius
案例 6:行height
= 60 并且scrollPosition.middle
=calendarTableView
单元格不再跟随cornerRadius
..
我不介意 60 的行高,但scrollPosition
必须是.middle 否则我不会看到当天的行..
你能看出我做错了什么吗?我搜索了其他帖子,但找不到任何可以让我知道问题所在的线索。像往常一样非常感谢。这是calendarTableView
功能:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell", for: indexPath) as! CalendarTableViewCell
// Configure the cell...
let date = datesArray[indexPath.row]
print(date)
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
cell.dayLabel.text = "\(String(describing: components.day!))" + " " + "\(dayNamesArray[components.weekday! - 1])"
cell.cellWeekday = components.weekday!
print("cell weekday is: \(cell.cellWeekday!)") // prints correct weekday
cell.cellId = "\(String(format:"%04d", components.year!))" + "\(String(format:"%02d", components.month!))" + "\(String(format:"%02d", components.day!))"
self.selectedDate = cell.cellId // used for time slots cellId
print("##################### selectedDate in tableview is :\(self.selectedDate) ")
// highlighting current day cell
if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
cell.dayLabel.backgroundColor = UIColor.red.withAlphaComponent(0.3)
// emulate user selecting the cell
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none) // changing to .middle makes the tableview go looping
print(" @@@@@@@@@@@@@@@@@@@@@@@@@@ selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")
// self.updateTimeSlots(selectedCell: cell)
// self.actualWeekday = cell.cellWeekday! // nil error
self.selectedDate = cell.cellId
// calculateOpenTimeSlots()
}
// let cornerRadius: CGFloat = 5
// cell.layer.cornerRadius = cornerRadius
// cell.clipsToBounds = true
// self.updateTimeSlots(selectedCell: cell)
return cell
}
解决方案
我决定不选择今天,row
而是只滚动calendarTableView
到那个中间位置。我也row
height
直接在heightForRowAt
. calendarTableView
现在的行为与预期的一样,并且没有直接选择单元格,而是按照我的实际需要突出显示。我希望这对其他人有帮助。最后的功能是:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell", for: indexPath) as! CalendarTableViewCell
let cornerRadius: CGFloat = 5
cell.layer.backgroundColor = UIColor.white.cgColor
cell.layer.cornerRadius = cornerRadius
cell.clipsToBounds = true
cell.dayLabel.textColor = UIColor.white
cell.dayLabel.layer.backgroundColor = UIColor.blue.withAlphaComponent(0.3).cgColor
cell.dayLabel.layer.cornerRadius = cornerRadius
cell.dayLabel.clipsToBounds = true
// Configure the cell...
let date = datesArray[indexPath.row]
print(date)
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
cell.dayLabel.text = "\(String(describing: components.day!))" + " " + "\(dayNamesArray[components.weekday! - 1])"
cell.cellWeekday = components.weekday!
print("cell weekday is: \(cell.cellWeekday!)") // prints correct weekday
cell.cellId = "\(String(format:"%04d", components.year!))" + "\(String(format:"%02d", components.month!))" + "\(String(format:"%02d", components.day!))"
self.selectedDate = cell.cellId // used for time slots cellId
print("##################### selectedDate in tableview is :\(self.selectedDate) ")
// highlighting current day cell
if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
// cell.layer.backgroundColor = UIColor.red.withAlphaComponent(0.3).cgColor
cell.dayLabel.layer.backgroundColor = UIColor.red.withAlphaComponent(0.3).cgColor
// emulate user selecting the cell
// tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.middle)
tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true)
print(" @@@@@@@@@@@@@@@@@@@@@@@@@@ selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")
self.updateTimeSlots(selectedCell: cell)
// self.actualWeekday = cell.cellWeekday! // nil error
self.selectedDate = cell.cellId
}
return cell
}
推荐阅读
- javascript - 无法获取 javascript 对象属性值
- oracle - 如何使用 SuiteScript 在 Netsuite 中获取记录的 XML?
- c++ - CEN/XFS:为什么 wAntiFraudModule 会给我这些值?
- python-3.x - 如何同时从 kv 文件和 py 文件将小部件添加到规则中?
- javascript - 如何从python文件执行onload函数javascript?
- delphi - RAD Studio 10.4.2:RAD 服务器和“复制点”图像或文本
- java - 实体关系设计以避免递归(多对多)
- javascript - 在 React 中实现高阶组件 - Redux
- android - 如何在android中均匀分布CardView
- python - 从一个 Airflow DAG 返回值到另一个