滚动时uitableview崩溃了

时间:2015-04-14 06:32:48

标签: ios objective-c uitableview

我正在我的应用程序的this示例的帮助下开发一个应用程序,一切都按照要求工作但当我扩展已扩展的单元格并向下滚动应用程序崩溃时,表格到达最后。让我在这里发布我的所有代码。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        FSParallaxTableViewCell *cell = nil;
        static NSString *cellIdentifier = nil;
        // init expanded cell
        if ([indexPath isEqual:self.expandedIndexPath]) {
            cellIdentifier = @"ExpandedCellIdentifier";
        }
        // init expanding cell
        else {
            cellIdentifier = @"Cell";
        }
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[FSParallaxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.cellImageView.image = [UIImage imageNamed:@"placeholder.png"];
        }
        if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
    cell.playIconBig = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            cell.playIconBig.tag= indexPath.row-1;
            [cell.playIconBig addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchDown];
            cell.playIconBig.frame = CGRectMake(25, 5, 25, 25);
            [cell.playIconBig setBackgroundImage:[UIImage imageNamed:@"play_expand.png"] forState:UIControlStateNormal];
            [cell.contentView addSubview:cell.playIconBig];
    }
        // set text in expanding cell
        if ([[cell reuseIdentifier] isEqualToString:@"Cell"]) {
            [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]] placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
            cell.clipsToBounds = YES;

            //[cell.contentView addSubview:arrowImg];

            cell.song.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
            cell.artist.text = [NSString stringWithFormat:@"By %@",[[rssOutputData objectAtIndex:indexPath.row]xmlsinger]];
            cell.share.text = [[rssOutputData objectAtIndex:indexPath.row]xmllikes];
            cell.download.text = [[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
    }
// set text in expanding cell
if ([[cell reuseIdentifier] isEqualToString:@"Cell"]) {
    [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]] placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
    cell.clipsToBounds = YES;

    //[cell.contentView addSubview:arrowImg];

    cell.song.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
    cell.artist.text = [NSString stringWithFormat:@"By %@",[[rssOutputData objectAtIndex:indexPath.row]xmlsinger]];
}
        return cell;
    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // take expanded cell into account when returning number of rows
    if (self.expandedIndexPath) {
        return [rssOutputData count] + 1;
    }
    return [rssOutputData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([indexPath isEqual:self.expandedIndexPath])
    {
        return 40;//height for expanded
    } else {
        return 140;//height for normal
    }
}

并且这里使用了电子注入方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.view addSubview:_internetConnectionIndicator];
    // disable touch on expanded cell
    UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPath];
    if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
        return;
    }

    // deselect row
    [tableView deselectRowAtIndexPath:indexPath
                             animated:NO];
    // get the actual index path
    indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
    // save the expanded cell to delete it later
    NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
    // same row tapped twice - get rid of the expanded cell
    if ([indexPath isEqual:self.expandingIndexPath]) {
        self.expandingIndexPath = nil;
        self.expandedIndexPath = nil;
    }
    // add the expanded cell
    else {
        self.expandingIndexPath = indexPath;
        self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
                                                    inSection:[indexPath section]];
    }

    [tableView beginUpdates];

    if (theExpandedIndexPath) {
        [theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
                            withRowAnimation:UITableViewRowAnimationNone];
    }
    if (self.expandedIndexPath) {
        [theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
                            withRowAnimation:UITableViewRowAnimationNone];
    }

    [tableView endUpdates];

    // scroll to the expanded cell
    [self.theTableView scrollToRowAtIndexPath:indexPath
                             atScrollPosition:UITableViewScrollPositionMiddle
                                     animated:YES];
}
- (NSIndexPath *)actualIndexPathForTappedIndexPath:(NSIndexPath *)indexPath
{
    if (self.expandedIndexPath && [indexPath row] > [self.expandedIndexPath row]) {
        return [NSIndexPath indexPathForRow:[indexPath row] - 1
                                  inSection:[indexPath section]];
    }

    return indexPath;
}

#pragma mark - UIScrollViewdelegate methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    for (FSParallaxTableViewCell *cell in self.theTableView.visibleCells) {
        [self updateImageViewCellOffset:cell];
    }
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self updateImageViewCellOffset:(FSParallaxTableViewCell *)cell];
}

请帮我调试我的代码。谢谢,这里是错误信息

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]'

3 个答案:

答案 0 :(得分:1)

我非常确定方法tableView:numberOfRowsInSection:返回错误的数字,它返回的数字大于数据大小。请仔细检查以下代码行:

if (self.expandedIndexPath) {
    return [rssOutputData count] + 1;
}

确保if语句的条件正确。有一种情况是您已将数据修改为其他地方但您不了解该数据。

答案 1 :(得分:1)

试试这个,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        FSParallaxTableViewCell *cell = nil;
        static NSString *cellIdentifier = nil;
        // init expanded cell
        if (indexPath.row >= [rssOutputData count])
            cellIdentifier = @"ExpandedCellIdentifier";
        }
        // init expanding cell
        else {
            cellIdentifier = @"Cell";
        }
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[FSParallaxTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.cellImageView.image = [UIImage imageNamed:@"placeholder.png"];
        }
        if (indexPath.row >= [rssOutputData count]){
    cell.playIconBig = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            cell.playIconBig.tag= indexPath.row-1;
            [cell.playIconBig addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchDown];
            cell.playIconBig.frame = CGRectMake(25, 5, 25, 25);
            [cell.playIconBig setBackgroundImage:[UIImage imageNamed:@"play_expand.png"] forState:UIControlStateNormal];
            [cell.contentView addSubview:cell.playIconBig];
    }else {
            [cell.cellImageView sd_setImageWithURL:[NSURL URLWithString:[[rssOutputData objectAtIndex:indexPath.row]xmllink]] placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
            cell.clipsToBounds = YES;

            //[cell.contentView addSubview:arrowImg];

            cell.song.text = [[rssOutputData objectAtIndex:indexPath.row]xmltitle];
            cell.artist.text = [NSString stringWithFormat:@"By %@",[[rssOutputData objectAtIndex:indexPath.row]xmlsinger]];
            cell.share.text = [[rssOutputData objectAtIndex:indexPath.row]xmllikes];
            cell.download.text = [[rssOutputData objectAtIndex:indexPath.row]xmldownloads];
    }
        return cell;
    }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // take expanded cell into account when returning number of rows
    if (self.expandedIndexPath) {
        return [rssOutputData count] + 1;
    }
    return [rssOutputData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([indexPath isEqual:self.expandedIndexPath])
    {
        return 40;//height for expanded
    } else {
        return 140;//height for normal
    }
}

答案 2 :(得分:0)

如果在向数据源添加+1之后返回行数而不是

if (self.expandedIndexPath) {
    return [rssOutputData count] + 1;
}

你应该确保对于附加行你不应该访问modal / rssOutputData应该被访问,直到它被绑定。如果某些代码如何访问数据不存在的索引的rssOutputData,那么你应该准备好看下面的错误

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]'

请重新操作您的代码,以某种方式尝试访问数组越界。