gpt4 book ai didi

ios - 奇怪的行为设置 tableView 行高和滚动位置 Swift

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

在同一个viewController中,我有一个代表月份的天数的calendarTableView。在cellForRowAt处,我选择与当天对应的行并调用函数来填充timeSlotCollectionView。这一切都有效,但我遇到了一些奇怪的情况。

情况 1:行 height = auto 且 scrollPosition.none = timeSlotCollectionView 不遵循 cornerRadius

情况 2:行 height = auto 且 scrollPosition.middle = 线程 1:calendarTableView 上的 EXC_BAD_ACCESS(code=2,address=0x659ef4)单元格定义

案例3:行height = 44和scrollPosition.none = calendarTableView单元格不遵循cornerRadius

情况 4:行 height = 44 和 scrollPosition.middle = calendarTableView 单元格不遵循 cornerRadius

情况 5:行 height = 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

}

最佳答案

我决定不选择今天的,而只滚动calendarTableView以使其处于中间位置。我还在 heightForRowAt 中直接声明 row heightcalendarTableView 现在的行为符合预期,并且单元格不是直接选择的,而是按照我实际想要的方式突出显示。我希望这会帮助其他人。最终的函数是:

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

}

关于ios - 奇怪的行为设置 tableView 行高和滚动位置 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654964/

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