【发布时间】:2017-01-25 17:30:35
【问题描述】:
我可以将我的 Person 对象随机分成 2 个数组,但我不知道如何在我的 tableview 上显示这些对。这是我的随机函数。
func randomizer(array: [Person]) {
guard let array = fetchedResultsController.fetchedObjects else { return }
let randomGenerator = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: array)
let splitSize = 2
let _ = stride(from: 0, to: randomGenerator.count, by: splitSize).map {
randomGenerator[$0..<min($0 + splitSize, randomGenerator.count)]
}
}
这是我的表格视图功能:
func numberOfSections(in tableView: UITableView) -> Int {
guard let sections = PersonController.sharedController.fetchedResultsController.sections else { return 0 }
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let sections = PersonController.sharedController.fetchedResultsController.sections else {
return 2 }
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}
【问题讨论】:
-
您看过
UITableView委托和数据源协议吗?这就是您将数据放入表格的方式。 -
我只是对其进行了编辑以显示这一点,但我对如何在每个部分中仅显示两行感到困惑。我想为每一对设置一个部分,然后在这些部分中显示这两个人对象
标签: ios swift uitableview core-data nsfetchedresultscontroller