这是我第一次处理连接到自定义委托(delegate)和数据源的 UITableView。直到今天,我习惯于连接到“ self ”。这道题的前传是here .
因此,我有两个额外的 UIView。使用分段控件,我将上面提到的 UIView 之一放在 self.view 上...
创建了两个 UITableView 子类并将它们设置为委托(delegate)和数据源。工作正常,没有泄漏或崩溃。检查是否在 segmentedControl 索引更改时初始化的类:
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
NSLog(@"Nominals got init");
}
return self;
}
NSLog 在更改段时起作用。
问题:
在我的自定义委托(delegate)类中,我覆盖了 UITableViewDelegate 和 UITableViewDataSource 协议(protocol)所需的方法。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(12, 12, 200, 12)];
lbl.text=@"some nominal";
cell.textLabel.text=@"Nominal";
[cell addSubview:lbl];
return cell;
}
目前我遇到异常:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
好吧,我的自定义委托(delegate)类中的单元格不是我创建的 UITableView 的同一个单元格吗:
-(void)populateNominals:(int)subCountryID
{
NominalsTableViewDelegate *del=[[NominalsTableViewDelegate alloc]init];
[self setNominalsDelegate:del];
UITableView *nominalsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 372) style:UITableViewStylePlain];
[nominalsTableView setDelegate:nominalsDelegate];
[nominalsTableView setDataSource:nominalsDelegate];
[nominalsTableView reloadData];
[self.nominalsView addSubview:nominalsTableView];
}
我的错误是什么?提前谢谢你。
最佳答案
您正在尝试使用 dequeueReusableCellWithIdentifier
dequeue
一个 cell
。如果队列中没有 cell
会发生什么?绝对崩溃。
所以你需要分配一个单元格,也添加这一行:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] init];
}
关于objective-c - iOS 5, Storyboard,ARC : UITableview doesn't get populated with data from custom delegate and datasource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097936/