将delegate和dataSource设置为UITableView

时间:2013-11-20 14:24:13

标签: ios objective-c uitableview static-methods

如何在静态方法中设置委托和dataSource

+(void)method
{
    UITableView *tv = [[UITableView alloc] init];
    tv.delegate = self;   // Warning: incompatible pointer type
    tv.dataSource = self; // Warning: incompatible pointer type
}

2 个答案:

答案 0 :(得分: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
}

二:如果不是单例类,修改方法签名以接受委托和数据源实例

+(void)methodWithDelegate:(id)delegate andDatasource:(id)datasource
{
    UITableView *tv = [[UITableView alloc] init];
    tv.delegate = delegate;
    tv.dataSource = datasource;
}

希望有所帮助。

答案 1 :(得分:0)

你的SingletonClass View控制器类是这样声明的吗?

@interface SingletonClass : UIViewController <UITableViewDelegate, UITableViewDataSource>

然而,你所做的似乎是一个糟糕的设计

相关问题