【发布时间】:2017-07-12 11:21:21
【问题描述】:
我正在写一个UICollectionViewLayout 子类。
我知道集合视图有多少个部分...numberOfSections。
我知道每个部分有多少项目...numberOfItems[section]。
给定一个起始 indexPath IndexPath(item: x, section: y) 我需要创建一个包含此起始 indexPath 之后的所有 indexPaths 的数组。
我尝试过类似...
// iterate all sections
let indexPaths: [IndexPath] = (initialIndexPath.section..<numberOfSections).flatMap { section in
// find initial item in section
let initialItemIndex = section == initialIndexPath.section ? initialIndexPath.item + 1 : 0
// iterate all items in section
return (initialItemIndex..<(numberOfItems[section] ?? 0)).flatMap { item in
return IndexPath(item: item, section: section)
}
}
但这告诉我(在第二个平面地图上)......
“flatMap”产生“[SegmentOfResult.Iterator.Element]”,而不是预期的上下文结果类型“IndexPath?”
我在布局的另一部分使用了类似的东西,但不太清楚为什么它在这里不起作用。
有更好的方法吗?
【问题讨论】:
标签: swift uicollectionviewlayout nsindexpath flatmap