numberOfRowsInSection if(section)error

时间:2014-01-31 22:21:01

标签: ios objective-c uitableview

我正在尝试在我的numberOfRowsInSection代码中使用if(section)方法。它看起来像这样:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0) {
        return 1;
    } else {
        if (movies && movies.count) {
            return movies.count;
        } else {
            return 0;
        }
    }
}

第一部分的工作方式类似于图像,第二部分是json解析的视频,称为电影。 但是当我启动应用程序时它会崩溃并给我这个错误:

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;
    }
}

1 个答案:

答案 0 :(得分:0)

据我所知,看起来这一行导致了问题:

cell = (PostsObject *)[nib objectAtIndex:0];

(就像它上面那样)。我愿意打赌你的NIB是空的。确保你有正确的名称。