在 calendarTableView 自定义单元格中添加视图破坏了代码 Swift 4

Posted

技术标签:

【中文标题】在 calendarTableView 自定义单元格中添加视图破坏了代码 Swift 4【英文标题】:Adding a view in calendarTableView custom cells broke the code Swift 4 【发布时间】:2019-10-18 09:01:47 【问题描述】:

我创建了一个带有自定义单元格的calendarTableVIew。单元格中只有一个dayLabel,并且我为内容视图分配了一个边框,只是为了勾勒出单元格和背景颜色。决定我想要一个单元格之间的空间,我添加了一个dayView,它现在有轮廓的边框,还有dayLabel在里面。 calendarTableview 具有不同颜色的当前日期单元格边框。 问题是,由于我将新的 dayView 添加到单元格中,因此当前日期单元格以及任何选定的单元格,当您选择任何一天时,现在都有黑色边框,并且 didDeselectdoesnt change it to default anymore. I tried to giveview@ 987654333@view, thedayViewborder, and thedayLabel`参数都受影响。你能看出那个黑色矩形是从哪里来的吗?我似乎找不到它。 一如既往,非常感谢您的时间和帮助。

这是单元格:

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

自定义单元格:

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
//            
//        
//    

表格视图:

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!")

    



【问题讨论】:

【参考方案1】:

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

这个单元格的层有时让我静止不动。所以在我的自定义单元格层是:

self(单元格本身) 内容视图 添加视图 标签

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

【讨论】:

以上是关于在 calendarTableView 自定义单元格中添加视图破坏了代码 Swift 4的主要内容,如果未能解决你的问题,请参考以下文章

请问怎么在excel单元格格式添加自定义

iOS - 如何在自定义单元格中使用继承?

如何在 iOS 中管理自定义单元格标签值?

在 TableView 自定义单元格中刷新数组

如何将自定义单元格放入另一个自定义单元格?

自定义 UITableViewCell 的 iOS 单元测试