【问题标题】:I am using FRC to display random pairs on my tableview我正在使用 FRC 在我的 tableview 上显示随机对
【发布时间】: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


【解决方案1】:

您如何跟踪数据模型中的配对?如果您将配对存储在数据模型中,您应该能够在该属性上使用您的 FRC sectionKey 来获取正确数量的部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多