首页 > 解决方案 > 奇怪的行为设置 tableView 行高和 scrollPosition Swift

问题描述

同样,viewController我有一个calendarTableView代表月份的日子。在cellForRowAt我选择与当天对应的行并调用一个函数来填充timeSlotCollectionView。一切正常,但我有一点奇怪的情况。

案例 1:行height= 自动和scrollPosition.none=timeSlotCollectionView不跟随cornerRadius

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

Case3:Row 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

    }

标签: iosswiftuitableviewuicollectionview

解决方案


我决定不选择今天,row而是只滚动calendarTableView到那个中间位置。我也row height直接在heightForRowAt. 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

    }

推荐阅读