簡體   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