Kis*_*der 0 static-methods objective-c uitableview ios
我如何在静态方法中设置委托和dataSource .
+(void)method
{
UITableView *tv = [[UITableView alloc] init];
tv.delegate = self; // Warning: incompatible pointer type
tv.dataSource = self; // Warning: incompatible pointer type
}
Run Code Online (Sandbox Code Playgroud)
小智 7
有两种可能性,
一:如果这是一个单例类(假设单例同时实现了数据源和委托方法)
+(void)method
{
UITableView *tv = [[UITableView alloc] init];
tv.delegate = [SingletonClass sharedInstance]; // This will return the singleton instance
tv.dataSource = [SingletonClass sharedInstance]; // This will return the singleton instance
}
Run Code Online (Sandbox Code Playgroud)
二:如果不是单例类,修改方法签名以接受委托和数据源实例
+(void)methodWithDelegate:(id)delegate andDatasource:(id)datasource
{
UITableView *tv = [[UITableView alloc] init];
tv.delegate = delegate;
tv.dataSource = datasource;
}
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
103 次 |
| 最近记录: |