Skip to main content
 首页 » 操作系统

swift之如何从 uipickerView : titleForRow 中获取 fetchedResultsController.objectAtIndex Path()

2024年01月01日37kuangbin

我有一个 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() 试图转换它接收到的参数?

我可能在这里做错了什么?

请您参考如下方法:

NSFetchedResultsController 使用索引路径(有一个 row 和一个 section 组件) 以便它可以用作具有多个部分的 TableView 的数据源。

在你的例子中,你可以从行号创建一个 NSIndexPath

let indexPath = NSIndexPath(forRow: row, inSection: 0) 
let task = self.fetchedResultsController.objectAtIndexPath(indexPath) as Task