尝试在Swift中将单元格分配给UICollectionView中的某些部分时,如何遍历数组?

时间:2018-09-15 15:36:05

标签: ios uicollectionview

我正在尝试使用cellForItemAt函数内部的for语句遍历数组。我遇到了麻烦,因为我需要在for语句之外存在return语句,但是我不确定如何正确执行此操作,同时仍然能够遍历任务数组以确保仅在正确的日期执行任务添加到适当的部分。在当前状态下,将返回一个普通的taskCell,我希望会发生这种情况-我只是这样做是为了使错误保持沉默。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let taskCell = collectionView.dequeueReusableCell(withReuseIdentifier: "taskCellID", for: indexPath) as! TaskCell

    let currentDate = Date()

    let day = DateComponents(calendar: nil, timeZone: nil, era: nil, year: nil, month: nil, day: indexPath.section, hour: nil, minute: nil, second: nil, nanosecond: nil, weekday: nil, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil)

    let date = Calendar.current.date(byAdding: day, to: currentDate)

    for task in 0...(userTasks.count - 1) {
        if userTasks[task].dueDate == date {
            taskCell.myTask = userTasks[indexPath.item]
            return taskCell
        }
    }

    return taskCell
}

0 个答案:

没有答案