我有两个不同的类,它们都实现了UITableViewDataSource
和UITableViewDelegate
协议。它们与我的UITableViewController
分开。
我想选择正确的数据源类在viewDidLoad()
中实例化,然后将UITableViewController设置为UITableViewDataSource
和UITableViewDelegate
类的委托。 (我将这些类中的对象返回到prepareForSegue
的UITableViewController,以了解在详细视图控制器屏幕中显示的内容。)
这不起作用。
在运行时,它会在没有运行时错误的情况下中断,只需使用“Thread 1:EXC_BAD_ACCESS(code = 1,address = ...)”行“class AppDelegate:UIResponder,UIApplicationDelegate {”
但是,如果我将数据源对象定义为UITableViewConroller中的实例变量(而不是在viewDidLoad()
中执行),那么它可以工作。当然,这会破坏目的,因为现在我无法切换到另一个数据源。
似乎如果我想将UITableViewController设置为委托(即,希望能够从数据源发回数据),那么由于某种原因我无法在viewDidLoad()中执行此操作。也许还没有完成创建对象呢? (如果我将对象创建为实例变量并立即初始化它们,则一切正常。)
protocol GroupByDelegator {
func callSegueFromGroupByDelegator()
}
class RemindersViewController: UITableViewController, GroupByDelegator {
@IBOutlet var remindersTableview: UITableView!
// var dataSource = GroupByNothingDataSource() // THIS WORKS, BUT THEN I CAN'T CHANGE THE DATASOURCE ANYMORE
var reminderWrapperToBeDisplayedInDetailView: ReminderWrapper?
override func viewDidLoad() {
super.viewDidLoad()
// if ... {
var dataSource = GroupByNothingDataSource() // BREAKS THE CODE
// } else {
// var dataSource = GroupByPriorityDataSource()
// }
dataSource.groupByDelegator = self // used so that the datasource can call the callSegueFromGroupByDelegator() func that will pass an object back to here.
self.tableView.dataSource = dataSource
self.tableView.delegate = dataSource
}
...
// This function is called by the data source delegates (GroupByNothingDataSource, GroupByPriorityDataSource) because they can't perform segues.
func callSegueFromGroupByDelegator(reminderWrapper: ReminderWrapper?) {
reminderWrapperToBeDisplayedInDetailView = reminderWrapper
//try not to send self, just to avoid retain cycles
self.performSegueWithIdentifier("reminderDetail", sender: tableView)
}
}
class GroupByPriorityDataSource: NSObject, UITableViewDataSource, UITableViewDelegate, TableViewCellDelegate, RemindersViewControllerDelegate {
var groupByDelegator: GroupByDelegator!
...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
...
// Pass object back to UITableViewController
self.groupByDelegator.callSegueFromGroupByDelegator(reminderWrapper?)
}
}
答案 0 :(得分:0)
我最终将两个数据源初始化为类实例变量(而不是根据用户交互动态地仅初始化一个。)现在,我根据用户交互动态地选择已初始化的数据源。
它解决了我的问题,但没有真正解决技术问题。这是平台中的错误吗?