我有一个包含五个部分的 UITableView。正如标题所描述的那样,cellForRowAtIndexPath 仅在前四个中被调用。已建立有关数据源和委托(delegate)的所有连接。此外,我的 numberOfSectionsInTableView 清楚地返回 5。从 cellForRowAtIndexPath 中打印出部分的数量显示正确的数字,从而确认 cellForRowAtIndexPath 根本没有被所有部分调用。这到底是怎么回事?我很努力地寻找这个问题的答案,但找不到。如果已经回答了这个问题,请原谅我并指出正确的方向。
我的 cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
switch (indexPath.section) {
case 0:
cell.textLabel.text = ticket.description;
break;
case 1:
cell.textLabel.text = ticket.ticketStatus;
break;
case 2:
cell.textLabel.text = ticket.priority;
break;
case 3:
cell.textLabel.text = ticket.customerOfficePhone;
break;
case 4: {
//This never ever gets executed
Comment *comment = [ticket.comments objectAtIndex:indexPath.row];
cell.textLabel.text = comment.commentContent;
break;
}
}
return cell;
}
我的 numberOfSectionsInTableView:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
我的 numberOfRowsInSection:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows;
if (section == 4) {
numberOfRows = [ticket.comments count];
}
else {
numberOfRows = 1;
}
return numberOfRows;
}
如有任何建议,我们将不胜感激。提前致谢。
最佳答案
啊哈!我想到了。我忘记了我已经为我的表格 View 硬编码了框架并将其添加为 subview ,禁用滚动到 ScrollView 。不幸的是, ScrollView 和表格 View 的高度都太小,无法容纳第五部分,我想这是除了第五部分之外的所有部分都被调用 cellForRowAtIndexPath 的原因。将我的表格 View 和 ScrollView 的高度重新调整得更大一点已经解决了我的问题。
关于ios - 未为所有部分调用 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9915401/