如何在对我的数组的 indexPath 调用中同时指定节和行?

Posted

技术标签:

【中文标题】如何在对我的数组的 indexPath 调用中同时指定节和行?【英文标题】:How can I specify both section and row in the indexPath call to my array? 【发布时间】:2015-02-12 03:37:03 【问题描述】:

以前,我使用以下代码填充我的 TableView:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath:
        indexPath) as CustomTableViewCell

    let entry = entries[indexPath.row]
    cell.entryLabel!.text = entry.labelOne
    cell.entryDayLabel!.text = entry.day
    cell.entryDateLabel!.text = entry.date

    return cell

我已经在我的表中添加了部分,但我无法弄清楚如何在此调用中指定部分和行。

我试过了

let entry = entries[indexPath.section]

我试过了

let entry = entries[indexPath.row + indexPath.section]

但两者都不能正常工作。

有没有我想念的正确方法来做到这一点?任何帮助是极大的赞赏。谢谢!

【问题讨论】:

【参考方案1】:
indexPath.section 

将返回当前部分。

你通常想要做的是添加一些条件逻辑,例如:

if(indexPath.section == 0)

    entry = entries[indexPath.row]

else if(indexPath.section == 1)

    entry = whateverDataSource[indexPath.row]

您的部分是否使用两种不同的表格和/或单元格类型?

【讨论】:

我的所有数据都存储在一个核心数据模型中,其中“部分”是属性之一。通过在 NSFetchedResultsController 中设置 sectionNameKeyPath 将数据分类为部分

以上是关于如何在对我的数组的 indexPath 调用中同时指定节和行?的主要内容,如果未能解决你的问题,请参考以下文章