繁体   English   中英

奇怪的行为设置tableView行高和scrollPosition Swift

[英]Weird behaviour setting tableView row height and scrollPosition Swift

在同一个viewController我有一个calendarTableView代表月份的日期。 cellForRowAt我选择对应于当前日期的行并调用一个函数来填充timeSlotCollectionView 这一切都有效,但我有一点奇怪的情况。

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

Case2:行height = auto和scrollPosition.middle =在calendarTableView单元格定义上的线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x659ef4)

Case3:行height = 44,scrollPosition.none = calendarTableView单元格不跟随cornerRadius

情况4:行height = 44, scrollPosition.middle = calendarTableView单元格不跟随cornerRadius

案例5:行heigh = 60, scrollPosition.none = calendarTableView单元格跟随cornerRadius

情况6:行height = 60并且scrollPosition.middle = calendarTableView单元格不要再次关注cornerRadius ..

我不介意行高60,但scrollPosition必须是。中间或我不会看到当天的行..

你能看出我做错了什么吗? 我搜索了其他帖子,但我找不到任何能让我知道问题所在的线索。 非常感谢像往常一样。 这是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而不是在那个中间位置。 我还直接在heightForRowAt声明row height 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

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM