当由dequeuereusablecell()

时间:2018-07-02 21:42:12

标签: uicollectionview interface-builder uicollectionviewcell xib nib

TableViewController中的一个单元格是自定义UITableViewCell,其中包含一个UICollectionView,该UICollectionView显示3个自定义UICollectionViewCells(请参见下文)。但是,在根数据单元格方法collectionView(cellForItemAt)中,当我尝试访问其中一个单元格的IBOutlet时,应用程序崩溃,并且线程1:致命错误:在展开一个可选值时意外发现nil 我尝试访问IBOutlet的属性的行上显示。无论IBOutlet和属性是否出现相同错误,应用程序都崩溃。但是,我能够访问UICollectionViewCell属性(例如背景色),而不能访问子类单元格中的属性。

RootData单元格(UITableView单元格包含一个显示3个单元格的“集合”视图) Root Table View in Interface Builder

CollectionView的插座 The Outlets for the Collection view contained in RootData

文件所有者的渠道 The Outlets for File Owner in Root Data

    class RootDateCell: UITableViewCell, UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var pageControl: UIPageControl!
    private var collectionViewCells: [UICollectionViewCell] = [UICollectionViewCell]()
    var selectedCourse: Course? = nil
    override func awakeFromNib() {
        super.awakeFromNib()
        collectionView.delegate = self
        collectionView.dataSource = self
        let flow = UICollectionViewFlowLayout()
        flow.scrollDirection = .horizontal
        flow.itemSize = CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
        collectionView.collectionViewLayout = flow
        collectionView.register(CourseStartDateSelection.self, forCellWithReuseIdentifier: "courseDateSelectionCell")
        collectionView.register(DueDateSelectionCollectionViewCell.self, forCellWithReuseIdentifier: "DueDateSelectionCell")
        collectionView.register(ReminderCollectionViewCell.self, forCellWithReuseIdentifier: "reminderSelectionCell")
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        print(collectionView.frame.width)
        print( collectionView.frame.height)
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        switch indexPath.row{
       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "courseDateSelectionCell", for: indexPath) as! CourseStartDateSelection
        cell.backgroundColor = UIColor.red
        return cell
    case 1:

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DueDateSelectionCell", for: indexPath) as! DueDateSelectionCollectionViewCell
        cell.backgroundColor = UIColor.blue

        return cell
    default:
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "reminderSelectionCell", for: indexPath) as! ReminderCollectionViewCell
        cell.backgroundColor = UIColor.green
        return cell
        }


    }




    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        pageControl.currentPage = Int(scrollView.contentOffset.x / scrollView.frame.width)
    }



}

DueDateSelectionCollectionViewCell (在RootData所包含的CollectionView内部显示的单元格)

In the Interface Builder

奥特莱斯

Outlets

文件所有者渠道

File Owner Outlets

class DueDateSelectionCollectionViewCell: UICollectionViewCell,AutomaticDateDelegate {



@IBOutlet weak var dueDatePicker: UIDatePicker!
@IBOutlet weak var automaticDateSwitch: UISwitch!
private var selectedCourse: Course? = nil

override func awakeFromNib() {
    super.awakeFromNib()
    print("ran")
}

@IBAction func switchFlipped(_ sender: UISwitch) {
    if(sender.isOn){
        dueDatePicker.date = selectedCourse!.nextStartTimeBasedOnDate(date: Date.init())
    }
}
func setSelectedCourse(selectedCourse: Course) {
    self.selectedCourse = selectedCourse
}


}

CourseStartDateSelection (在RootData包含的CollectionView中显示的另一个单元格)

class CourseStartDateSelection: UICollectionViewCell, AutomaticDateDelegate, UIPickerViewDelegate,UIPickerViewDataSource {
private var selectedCourse: Course? = nil
private var nextTenCourseTimes: [Date]? = nil
@IBOutlet weak var futureCourseTimesPicker: UIPickerView!

override func awakeFromNib() {
    super.awakeFromNib()
    futureCourseTimesPicker.delegate = self
    futureCourseTimesPicker.dataSource = self

    // Initialization code
}

func setSelectedCourse(selectedCourse: Course) {
    self.selectedCourse = selectedCourse
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return 10
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return getFormattedDate(date: nextTenCourseTimes![row])
}

ReminderCollectionViewCell (在RootData包含的CollectionView中显示的另一个单元格) [没有代码,.xib仅显示标签]

表明我可以更改UICollectionViewCell的背景颜色,但不会显示UIKit元素,如果我以编程方式访问它们的插座,应用程序将崩溃。 in simulator

0 个答案:

没有答案