【问题标题】:Weird behaviour setting tableView row height and scrollPosition Swift奇怪的行为设置 tableView 行高和 scrollPosition Swift
【发布时间】:2019-04-12 15:26:39
【问题描述】:

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

案例 1:行 height = auto 和 scrollPosition.none = timeSlotCollectionView 不关注 cornerRadius

案例 2:行 height = auto 和 scrollPosition.middle = 线程 1:calendarTableViewcell 定义上的 EXC_BAD_ACCESS(代码=2,地址=0x659ef4)

案例3:行height = 44 和scrollPosition.none = calendarTableViewcell 不关注cornerRadius

案例 4:行 height = 44 和 scrollPosition.middle = calendarTableViewcell 不关注 cornerRadius

案例 5:行 height = 60 和 scrollPosition.none = calendarTableViewcell 跟随 cornerRadius

案例 6:行 height = 60 和 scrollPosition.middle = calendarTableViewcell 不要再次关注 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

    }

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    我决定不选择今天的row,而只滚动calendarTableView以使其处于中间位置。我还直接在heightForRowAt 中声明row heightcalendarTableView 现在按预期运行,并且单元格不是直接选择的,而是按照我的实际需要突出显示。 我希望这对其他人有帮助。 最后的功能是:

    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
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      相关资源
      最近更新 更多