Ios 如果(节)错误,则节中的行数
我试图在numberOfRowsInSection代码中使用if(section)方法。看起来是这样的:Ios 如果(节)错误,则节中的行数,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我试图在numberOfRowsInSection代码中使用if(section)方法。看起来是这样的: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section==0) { return 1; } else { if (movies && movies.count) {
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0) {
return 1;
} else {
if (movies && movies.count) {
return movies.count;
} else {
return 0;
}
}
}
第一部分的工作方式类似于图像,第二部分是一个json解析的提要,称为movies。
但是,当我启动应用程序时,它崩溃并给我以下错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
有人能解释一下原因吗
编辑
cellForRowAtIndexPath代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
static NSString *Identifier1 = @"TableHeaderView";
// cell type 1
TableHeaderView *cell = [tableView dequeueReusableCellWithIdentifier:Identifier1];
if (cell == nil) {
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"TableHeaderView" owner:self options:nil];
cell = (TableHeaderView *)[nib objectAtIndex:0];
}
//set the image on the cell
return cell;
} else {
static NSString *Identifier2 = @"PostsObject";
// cell type 2
PostsObject *cell = [tableView dequeueReusableCellWithIdentifier:Identifier2];
if (cell == nil) {
NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"PostsObject" owner:self options:nil];
cell = (PostsObject *)[nib objectAtIndex:0];
}
NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];
NSString *strText = [movie objectForKey:[self getTextKey]];
CGRect rect = cell.title.frame;
rect.size.height = [self getHeightForText:strText];
cell.title.frame = rect;
cell.title.text = strText;
cell.arrow.center = CGPointMake(cell.arrow.frame.origin.x, rect.origin.y + rect.size.height/2);
cell.published.text = [movie objectForKey:[self getPostedTime]];
cell.twitterName.text = [movie objectForKey:[self getTwitterName]];
return cell;
}
}
据我所知,这条线似乎是问题的根源:
cell = (PostsObject *)[nib objectAtIndex:0];
(和它上面一样的那个)。我敢打赌你的笔尖是空的。请确保您有正确的名称。是的,请将您的
cellForRowAtIndexPath:
代码张贴到。旁注-如果块,您不需要第二个。只需执行返回电影即可代码>。如果movies
为nil
,则返回0。可能数组为空??可能您应该查看异常回溯以查看异常发生的位置??相对于movie数组,它看起来正常。你能设置一个异常断点吗:断点选项卡,左下角的加号。不,NIB现在是空的,它是正确的名称。应用程序似乎在返回2行时崩溃;在numberOfSectionsInTableView方法中。在这种情况下,您能否显示//在单元格上设置图像后的代码。因为如果我只写返回电影;在numberOfRowsInSection函数中,它返回json中的所有项,还返回TableHeaderView中相同数量的图像。所以这部分的数字有点问题。。。