UITableView内容高度问题导致应用程序崩溃

时间:2012-01-10 03:40:55

标签: iphone objective-c ios xcode ipad

我的视图中有两个UITableViews显示相同的内容(圣经经文),但是在不同的语言中,top tableview显示英语和底部表格显示hindi.everything工作正常,但是这一节的一些章节在uitableview中加载数据应用程序崩溃,错误在这个区域

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

    CGSize textSize = [[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"Georgia" size:18.0 ]  constrainedToSize:CGSizeMake(280.0f,MAXFLOAT)   lineBreakMode:UILineBreakModeWordWrap];

    return textSize.height +20;

    CGSize textSizehindi = [[tempArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont fontWithName:@"testfont" size:18.0 ]   constrainedToSize:CGSizeMake(280.0f,MAXFLOAT)   lineBreakMode:UILineBreakModeWordWrap];

    return textSizehindi.height +20;

}

并且在uitableview中播放了太多时间后,它会导致UITableViewCells滚动速度变慢。没有平滑的滚动问题。上面的代码中有任何eroor,我在加载某些章节时遇到错误。 提前致谢。 编辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *CellIdentifier = @"Cell";

    readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"readCell" owner:self options:nil]; 
        cell = [nib objectAtIndex:0]; 

        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;

    }
    if(tableView == table)
    {
      UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
         myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
        [myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
       cell.selectedBackgroundView = myBackView;
        [myBackView release];


        table.backgroundColor = [UIColor clearColor];
        table.separatorColor = [UIColor clearColor];

        cell.chapterAndVerse.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
        cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:17.0];
        cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
        cell.textLabel.text =  [NSString stringWithFormat:@"  %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];

        cell.textLabel.font = [UIFont fontWithName:@"Georgia" size:18];

        cell.backgroundColor = [UIColor clearColor];

    }




    else if(tableView == tab)
    {
        UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
        myBackView.backgroundColor = [UIColor colorWithRed:250.0 green:248.0 blue:192.0 alpha:1.0];
        [myBackView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:0.75 alpha:1]];
        cell.selectedBackgroundView = myBackView;
        [myBackView release];

        tab.backgroundColor = [UIColor clearColor];
        tab.separatorColor = [UIColor clearColor];

        cell.chapterAndVerse.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
        cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:17.0];
        cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
        cell.textLabel.text =  [NSString stringWithFormat:@"  %@",[tempArray objectAtIndex:indexPath.row]];

        cell.textLabel.font = [UIFont fontWithName:@"testfont" size:18];

        cell.backgroundColor = [UIColor clearColor];

    }
      return cell; 

}

EDIT2

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == table) {
        return [delegate.allSelectedVerseEnglish count];
    }
    else if (tableView == tab )
    {
        return [tempArray count];

    }



}

3 个答案:

答案 0 :(得分:1)

首先做一件事。在使用你的两个数组之前,NSLog在重新加载表之前的两个数组。并检查两个数组是否具有相同数量的对象。这可能是崩溃的原因。

答案 1 :(得分:0)

在上面的代码中首先添加返回英文和印地文的文本大小的条件。因为目前你总是返回第一个英语诗歌的文字大小。你面临的主要问题不在上面的代码中。问题应该在你的另一个表视图委托调用上: - CellForRowAtIndexPath。

你可以在这里粘贴你的CellForRowAtIndexPath委托调用,这样我可以给你更多的想法。

答案 2 :(得分:0)

永远不会调用heightForRowAtIndexPath方法的第二部分。它始终返回return textSize.height +20;最可能的崩溃方式是空指针delegate.allSelectedVerseEnglish或不存在的字体。