如何从pickerView:titleForRow中获取fetchedResultsController.objectAtIndexPath()

时间:2014-10-20 17:50:29

标签: swift uipickerview nsfetchedresultscontroller

我有一个UIPickerView,它的数据源是fetchedResultsController。

我的代表功能:

   func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String
{
    let task: Task = self.fetchedResultsController.objectAtIndexPath(row) as Task
    return task.name
}

导致此错误:

'NSNumber' is not a subtype of 'NSIndexPath'

我尝试在传递之前将行强制转换为NSIndexPath,这导致了同样的错误:

 'NSNumber' is not a subtype of 'NSIndexPath'

我猜测fetchedResultsController.objectAtIndexPath()会尝试转换它收到的参数吗?

我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

NSFetchedResultsController使用索引路径(具有部分组件) 这样它就可以用作具有多个部分的表视图的数据源。

在您的情况下,您可以使用

从行号创建NSIndexPath
let indexPath = NSIndexPath(forRow: row, inSection: 0)
let task = self.fetchedResultsController.objectAtIndexPath(indexPath) as Task