【发布时间】:2013-12-06 18:12:21
【问题描述】:
我想创建一个具有标准 tableview 数据源方法(-cellForRowAtIndexPath:、-numberOfRows 等)的默认实现的 UIViewController 类。
我想创建一个自定义下拉列表。
这是我的问题的详细说明:
我定义了MyDropDownListController 类,它实现了UITableViewDelegate 和UITableViewDataSource 协议,其中UITableView ivar 称为table。
然后,我定义一个自定义的配置方法:
-(void)initDropDownListWithItems:(NSArray*)items andAddToView:(UIView*)view
{
dropDownItems = items; //data source array of items to display in list
table = [[UITableView alloc] initWithFrame: CGRectMake(view.frame.origin.x, view.frame.origin.y + view.frame.size.height + 1, view.frame.size.width, 140)];
[view addSubview:table]; //add my table to view
[table setDataSource:self];
[table setDelegate:self];
[table reloadData];
}
然后我定义-numberOfRowsInSection:和cellForRowAtIndexPath等
在此之后,在其他一些 UIViewController 类中(我想要我的下拉列表)我有这个:
dataSource = @[@"first", @"second", @"third"];
MyDropDownListController * list= [[MyDropDownListController alloc] init];
[list initDropDownListWithItems:dataSource andAddToView:someViewInThisController];
启动应用程序后,显示此列表时出现奇怪的异常,例如:
-[_UIParallaxDimmingView tableView:numberOfRowsInSection:]:无法识别的选择器发送到实例 0x16500c70
如您所见,MyDropDownListController 并没有调用数据源方法,而是为其他一些奇怪的事情调用了数据源方法。
这些消息可能会有些不同:
[UIBarButtonItem tableView:numberOfRowsInSection:]: 无法识别 发送选择器
等等。
希望您能看到问题所在。
正如我在这里所说的那样,MyDropDownListController 似乎没有调用数据源方法:
[table setDataSource:self];
但对于其他一些东西。
【问题讨论】:
-
您能否在代码中的某个位置再次设置表的数据源?是否在
MyDropDownListController中实现了所需的数据源方法? -
@BlackRider 不,我只是在这个
initDropDown方法中第一次设置它们。是的,我说过Then I define numberOfRowsInSection: and cellForRowAtIndexPath and etc. -
很奇怪。
[table reloadData]内部的initDropDownListWithItems调用是否成功? -
@BlackRider 这个问题即使我不打电话
[table reloadData] -
@BlackRider 我从帖子中删除了这个电话,以免误导。正如我所理解的那样,这些方法中的自我问题?因为代替它的方法接收者是一些其他未知对象
标签: ios objective-c cocoa-touch uitableview ios7