gpt4 book ai didi

ios - 在 calendarTableView 自定义单元格中添加 View 破坏了代码 Swift 4

转载 作者:行者123 更新时间:2023-11-29 05:23:22 24 4
gpt4 key购买 nike

我创建了一个 calendarTableVIew与自定义单元格。细胞只有 dayLabel在它们中,对于内容 View ,我分配了一个边框只是为了勾勒出单元格和背景颜色。决定我想要单元格之间有一个空格,我添加了 dayView现在有轮廓的边框,以及 dayLabel在里面。calendarTableview当前单元格边框具有不同的颜色。问题是,自从我添加了新的 dayView对于单元格,当前日期单元格以及任何选定的单元格,当您选择任何一天时,现在有一个黑色边框,并且 didDeselect没有t change it to default anymore.
I tried to give
查看( contentView ) a rounded outline to see what effects it would produce and I can indeed see the rounded查看, the日视border, and the dayLabel`参数全部受影响。你能看出那个黑色矩形是从哪里来的吗?我好像找不到。一如既往,非常感谢您的时间和帮助。

这是单元格:

enter image description here enter image description here

这是代码(注释掉的部分是添加新 View 之前的代码):

自定义单元格:

class CalendarTableViewCell: UITableViewCell {


@IBOutlet weak var view: UIView! // added after errror has began but didn't solve it
@IBOutlet weak var dayView: UIView!
@IBOutlet weak var dayLabel: UILabel!


var cellDate : String!
var cellId: String!
var cellWeekday: Int!

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

override func prepareForReuse() {
super.prepareForReuse()
// Set your default background color, title color etc
configureUi()
}
func configureUi() {
self.backgroundColor = UIColor.red
self.isOpaque = false
view.layer.cornerRadius = 5
view.layer.borderWidth = 0
view.clipsToBounds = true
dayView.layer.cornerRadius = 5
dayView.layer.borderWidth = 2
dayView.clipsToBounds = true
if Theme.selectedTheme == 1 {
if #available(iOS 11.0, *) {
view.layer.backgroundColor = UIColor.clear.cgColor//Theme.textFieldBackgroundColor?.cgColor//backgroundColor?.cgColor
view.layer.borderColor = UIColor.purple.cgColor
dayView.layer.backgroundColor = Theme.textFieldBackgroundColor?.cgColor
dayView.layer.borderColor = Theme.secondTintColor?.cgColor

dayLabel.layer.backgroundColor = Theme.textFieldBackgroundColor?.cgColor//labelBackgroundColor?.cgColor
dayLabel.textColor = Theme.textFieldTextColor//labelTextColor
} else {
// Fallback on earlier versions
layer.backgroundColor = Theme.textFieldBackgroundColorRgb.cgColor//backgroundColorRgb.cgColor

dayView.layer.backgroundColor = Theme.textFieldBackgroundColorRgb.cgColor//backgroundColorRgb.cgColor
dayView.layer.borderColor = Theme.secondTintColorRgb.cgColor//calendarCellRgb.cgColor

dayLabel.layer.backgroundColor = Theme.textFieldBackgroundColorRgb.cgColor//labelBackgroundColorRgb.cgColor
dayLabel.textColor = Theme.textFieldTextColorRgb//labelTextColorRgb
}
} else if Theme.selectedTheme == 2 {
if #available(iOS 11.0, *) {
layer.borderColor = Theme.calendarCell2?.cgColor
layer.backgroundColor = Theme.backgroundColor2?.cgColor
dayLabel.layer.backgroundColor = Theme.labelBackgroundColor2?.cgColor
dayLabel.textColor = Theme.labelTextColor2
} else {
// Fallback on earlier versions
layer.borderColor = Theme.calendarCellRgb2.cgColor
layer.backgroundColor = Theme.backgroundColorRgb2.cgColor
dayLabel.layer.backgroundColor = Theme.labelBackgroundColorRgb2.cgColor
dayLabel.textColor = Theme.labelTextColorRgb2
}
}
}

// func configureUi() {
// layer.cornerRadius = 5
// layer.borderWidth = 2
// clipsToBounds = true
// if Theme.selectedTheme == 1 {
// if #available(iOS 11.0, *) {
// layer.borderColor = Theme.calendarCell?.cgColor
// layer.backgroundColor = Theme.backgroundColor?.cgColor
// dayLabel.layer.backgroundColor = Theme.labelBackgroundColor?.cgColor
// dayLabel.textColor = Theme.labelTextColor
// } else {
// // Fallback on earlier versions
// layer.borderColor = Theme.calendarCellRgb.cgColor
// layer.backgroundColor = Theme.backgroundColorRgb.cgColor
// dayLabel.layer.backgroundColor = Theme.labelBackgroundColorRgb.cgColor
// dayLabel.textColor = Theme.labelTextColorRgb
// }
// } else if Theme.selectedTheme == 2 {
// if #available(iOS 11.0, *) {
// layer.borderColor = Theme.calendarCell2?.cgColor
// layer.backgroundColor = Theme.backgroundColor2?.cgColor
// dayLabel.layer.backgroundColor = Theme.labelBackgroundColor2?.cgColor
// dayLabel.textColor = Theme.labelTextColor2
// } else {
// // Fallback on earlier versions
// layer.borderColor = Theme.calendarCellRgb2.cgColor
// layer.backgroundColor = Theme.backgroundColorRgb2.cgColor
// dayLabel.layer.backgroundColor = Theme.labelBackgroundColorRgb2.cgColor
// dayLabel.textColor = Theme.labelTextColorRgb2
// }
// }
// }

}

表格 View :

extension WorkshopBookingsViewController: UITableViewDataSource, UITableViewDelegate {

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datesArray.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell", for: indexPath) as! CalendarTableViewCell
// let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell") as! CalendarTableViewCell
cell.configureUi()
let date = datesArray[indexPath.row]
cell.cellDate = String( describing: date)
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let dayInWeek = dateFormatter.string(from: date)
// cell.dayLabel.text = "\(String(describing: components.day!))" + " " + "\(dayNamesArray[components.weekday! - 1])"
cell.dayLabel.text = "\(String(describing: components.day!))" + " " + dayInWeek
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.borderWidth = 4
if Theme.selectedTheme == 1 {
if #available(iOS 11.0, *) {

cell.dayLabel.backgroundColor = UIColor.blue
cell.dayLabel.layer.borderColor = UIColor.purple.cgColor
// cell.contentView.backgroundColor = UIColor.blue
cell.dayView.layer.borderColor = UIColor.blue.cgColor//Theme.firstTintColor?.cgColor
} else {
// Fallback on earlier versions
cell.dayView.layer.borderColor = Theme.firstTintColorRgb.cgColor
}

} else if Theme.selectedTheme == 2 {
if #available(iOS 11.0, *) {
cell.layer.borderColor = Theme.calendarCellToday2?.cgColor
} else {
// Fallback on earlier versions
cell.layer.borderColor = Theme.calendarCellTodayRgb2.cgColor
}
}
// print(" @@@@@@@@@@@@@@@@@@@@@@@@@@ selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")
self.selectedDate = cell.cellId
self.updateTimeSlots(selectedCell: cell)
}
// if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
//
// cell.layer.borderWidth = 4
// if Theme.selectedTheme == 1 {
// if #available(iOS 11.0, *) {
// cell.layer.borderColor = Theme.calendarCellToday?.cgColor
// } else {
// // Fallback on earlier versions
// cell.layer.borderColor = Theme.calendarCellTodayRgb.cgColor
// }
//
// } else if Theme.selectedTheme == 2 {
// if #available(iOS 11.0, *) {
// cell.layer.borderColor = Theme.calendarCellToday2?.cgColor
// } else {
// // Fallback on earlier versions
// cell.layer.borderColor = Theme.calendarCellTodayRgb2.cgColor
// }
// }
// // print(" @@@@@@@@@@@@@@@@@@@@@@@@@@ selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")
// self.selectedDate = cell.cellId
// self.updateTimeSlots(selectedCell: cell)
// }
return cell
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? CalendarTableViewCell {
cell.configureUi()
// highlighting current day cell
if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
cell.dayView.layer.borderWidth = 4
if Theme.selectedTheme == 1 {
if #available(iOS 11.0, *) {
cell.dayView.layer.borderColor = Theme.firstTintColor?.cgColor
} else {
// Fallback on earlier versions
cell.dayView.layer.borderColor = Theme.firstTintColorRgb.cgColor
}
} else if Theme.selectedTheme == 2 {
if #available(iOS 11.0, *) {
cell.dayView.layer.borderColor = Theme.calendarCellToday2?.cgColor
} else {
// Fallback on earlier versions
cell.layer.borderColor = Theme.calendarCellTodayRgb2.cgColor
}
}
// print(" @@@@@@@@@@@@@@@@@@@@@@@@@@ selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")

self.selectedDate = cell.cellId
}
}


// if let cell = tableView.cellForRow(at: indexPath) as? CalendarTableViewCell {
// cell.configureUi()
// // highlighting current day cell
// if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
// cell.layer.borderWidth = 4
// if Theme.selectedTheme == 1 {
// if #available(iOS 11.0, *) {
// cell.layer.borderColor = Theme.calendarCellToday?.cgColor
// } else {
// // Fallback on earlier versions
// cell.layer.borderColor = Theme.calendarCellTodayRgb.cgColor
// }
// } else if Theme.selectedTheme == 2 {
// if #available(iOS 11.0, *) {
// cell.layer.borderColor = Theme.calendarCellToday2?.cgColor
// } else {
// // Fallback on earlier versions
// cell.layer.borderColor = Theme.calendarCellTodayRgb2.cgColor
// }
// }
// // print(" @@@@@@@@@@@@@@@@@@@@@@@@@@ selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")
//
// self.selectedDate = cell.cellId
// }
// }
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? CalendarTableViewCell {
cell.layer.borderWidth = 4
if Theme.selectedTheme == 1 {
if #available(iOS 11.0, *) {
cell.dayView.layer.borderColor = Theme.backgroundColor?.cgColor
} else {
// Fallback on earlier versions
cell.dayView.layer.borderColor = Theme.firstTintColorRgb.cgColor
}

} else if Theme.selectedTheme == 2 {
if #available(iOS 11.0, *) {

cell.dayView.layer.borderColor = Theme.firstTintColor2?.cgColor
} else {
// Fallback on earlier versions
cell.dayView.layer.borderColor = Theme.firstTintColorRgb2.cgColor
}
}
self.updateTimeSlots(selectedCell: cell)
}

// if let cell = tableView.cellForRow(at: indexPath) as? CalendarTableViewCell {
// cell.layer.borderWidth = 4
// if Theme.selectedTheme == 1 {
// if #available(iOS 11.0, *) {
// cell.layer.borderColor = Theme.firstTintColor?.cgColor
// } else {
// // Fallback on earlier versions
// cell.layer.borderColor = Theme.firstTintColorRgb.cgColor
// }
//
// } else if Theme.selectedTheme == 2 {
// if #available(iOS 11.0, *) {
//
// cell.layer.borderColor = Theme.firstTintColor2?.cgColor
// } else {
// // Fallback on earlier versions
// cell.layer.borderColor = Theme.firstTintColorRgb2.cgColor
// }
// }
// self.updateTimeSlots(selectedCell: cell)
// }
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { // Error: this causes multiple index paths to be dequeed at tableview reload. call directly in cellForRowAt instead
// let cell: CalendarTableViewCell = tableView(self.calendarTableview, cellForRowAt: IndexPath.init(row: self.actualDay - 1, section: 0)) as! CalendarTableViewCell
// self.updateTimeSlots(selectedCell: cell)

}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
// print("scrollViewDidEndDecelerating")
}

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate {
// print("scrollViewDidEndDragging")
}
}

func scrollingFinish() -> Void {
// print("Scroll Finished!")

}


}

最佳答案

发现问题。当我添加新的 dayView View 时,我为单元格本身指定了 self.backgroundColor ,但我还应该指定 self.layer.borderColor = UIColor。 clear.cgColor,否则将获得默认的黑色。

这个细胞层有时会让我安静下来。所以在我的自定义单元格中,层是:

self(细胞本身)内容 View 添加 View 标签

并且您必须为每个图层指定backgroundColorborderColor。希望这对其他人有帮助。干杯。

关于ios - 在 calendarTableView 自定义单元格中添加 View 破坏了代码 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58447430/

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