Ios 在calendarTableView自定义单元格中添加视图会破坏代码Swift 4
我用自定义单元格创建了一个Ios 在calendarTableView自定义单元格中添加视图会破坏代码Swift 4,ios,swift,uitableview,Ios,Swift,Uitableview,我用自定义单元格创建了一个calendarTableVIew。单元格中只有一个dayLabel,我给内容视图分配了一个边框,用于勾勒单元格和背景色的轮廓。决定在单元格之间留一个空格,我添加了一个dayView,它现在有了轮廓的边框,并在其中添加了dayLabel。 日历表视图的当前日期单元格边框颜色不同。 问题是,由于我在单元格中添加了新的dayView,因此当您选择任何一天时,当前的单元格以及任何选定的单元格现在都有一个黑色边框,并且didDeselect不再将其更改为默认值。 我试着给vie
calendarTableVIew
。单元格中只有一个dayLabel
,我给内容视图分配了一个边框,用于勾勒单元格和背景色的轮廓。决定在单元格之间留一个空格,我添加了一个dayView
,它现在有了轮廓的边框,并在其中添加了dayLabel
。
日历表视图的当前日期单元格边框颜色不同。
问题是,由于我在单元格中添加了新的dayView
,因此当您选择任何一天时,当前的单元格以及任何选定的单元格现在都有一个黑色边框,并且didDeselect
不再将其更改为默认值。
我试着给view(contentView)一个完整的轮廓,看看它会产生什么效果,我确实可以看到完整的
view,
dayView边框和
dayLabel`参数都受到了影响。你能看到那个黑色矩形是从哪里来的吗?我好像找不到它。
一如既往,非常感谢您的时间和帮助
这是牢房:
这是代码(注释掉的部分是添加新视图之前的代码):
自定义单元格:
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
// }
// }
// }
}
Tableview:
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
视图时,我为单元格本身指定了self.backgroundColor
,但我还应该指定一个self.layer.borderColor=UIColor.clear.cgColor
,否则它将获得默认的黑色
这个细胞层有时会让我安静。因此,在我的自定义单元格中,图层是:
自我(细胞本身)
内容视图
添加视图
标签
您必须为每个层指定backgroundColor
和borderColor
。
希望这对其他人有帮助。
干杯