这段代码中的错误是什么,它崩溃了?

时间:2011-09-13 07:00:09

标签: iphone uitableview custom-cell

我正在使用表格视图,其中我按自定义单元格显示10行表格,其中一行是普通单元格。我已经使用了这段代码......

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


    static NSString *CellIdentifier=@"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (indexPath.row == [listofBusiness count]) {

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            }

    cell.textLabel.text= @"Show more...";
    //cell = self.tvcell;
    return cell;

}

else if(indexPath.row==[appDelegate.busines_Aray count])
{

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    return cell;

}

if(customCell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"Place_cell" owner:self options:nil];
}


// Configure the cell...


    cell.backgroundColor=[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    BusinessData *business_datas = [appDelegate.busines_Aray objectAtIndex:indexPath.row];  
    UITextField *tmptxt;
    tmptxt = (UITextField *)[cell viewWithTag:1];
    tmptxt.backgroundColor=[UIColor clearColor];
    tmptxt.text = [NSString stringWithFormat:@"%@",business_datas.name_business ];
    tmptxt = (UITextField *)[cell viewWithTag:2];
    tmptxt.text = [NSString stringWithFormat:@"%@", business_datas.cat_business];   
    tmptxt = (UITextField *)[cell viewWithTag:3];
    tmptxt.backgroundColor=[UIColor clearColor];
    tmptxt.text = [NSString stringWithFormat:@"%d",business_datas.noofreview];

    if(business_datas.avg_rating ==0.0)
    {
        CGRect myImage1 =CGRectMake(190,5,20,22);
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
        [imageView1 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView1];
        [imageView1 release]; 
        CGRect myImage2 =CGRectMake(210,5,20,22);
        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
        [imageView2 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView2];
        [imageView2 release]; 
        CGRect myImage3 =CGRectMake(230,5,20,22);
        UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
        [imageView3 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView3];
        [imageView3 release]; 
        CGRect myImage4 =CGRectMake(250,5   ,20,22);
        UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
        [imageView4 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView4];
        [imageView4 release]; 
        CGRect myImage5 =CGRectMake(270,5,20,22);
        UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
        [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView5];
        [imageView5 release]; 

    }
    else if(business_datas.avg_rating ==0.5)
    {
        CGRect myImage1 =CGRectMake(190,5,20,22);
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
        [imageView1 setImage:[UIImage imageNamed:@"Black-Half.png"]];
        [cell addSubview:imageView1];
        [imageView1 release]; 
        CGRect myImage2 =CGRectMake(210,5,20,22);
        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
        [imageView2 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView2];
        [imageView2 release]; 
        CGRect myImage3 =CGRectMake(230,5,20,22);
        UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
        [imageView3 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView3];
        [imageView3 release]; 
        CGRect myImage4 =CGRectMake(250,5,20,22);
        UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
        [imageView4 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView4];
        [imageView4 release]; 
        CGRect myImage5 =CGRectMake(270,5,20,22);
        UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
        [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView5];
        [imageView5 release]; 

    }
    else if(business_datas.avg_rating ==1.0)
    {
        CGRect myImage1 =CGRectMake(190,5,20,22);
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
        [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
        [cell addSubview:imageView1];
        [imageView1 release]; 
        CGRect myImage2 =CGRectMake(210,5,20,22);
        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
        [imageView2 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView2];
        [imageView2 release]; 
        CGRect myImage3 =CGRectMake(230,5,20,22);
        UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
        [imageView3 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView3];
        [imageView3 release]; 
        CGRect myImage4 =CGRectMake(250,5,20,22);
        UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
        [imageView4 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView4];
        [imageView4 release]; 
        CGRect myImage5 =CGRectMake(270,5,20,22);
        UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
        [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView5];
        [imageView5 release]; 

    }
    else if(business_datas.avg_rating ==1.5)
    {
        CGRect myImage1 =CGRectMake(190,5,20,22);
        UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
        [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
        [cell addSubview:imageView1];
        [imageView1 release]; 
        CGRect myImage2 =CGRectMake(210,5,20,22);
        UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
        [imageView2 setImage:[UIImage imageNamed:@"Black-Half.png"]];
        [cell addSubview:imageView2];
        [imageView2 release]; 
        CGRect myImage3 =CGRectMake(230,5,20,22);
        UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
        [imageView3 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView3];
        [imageView3 release];       
        CGRect myImage4 =CGRectMake(250,5,20,22);
        UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
        [imageView4 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView4];
        [imageView4 release]; 
        CGRect myImage5 =CGRectMake(270,5,20,22);
        UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
        [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
        [cell addSubview:imageView5];
        [imageView5 release]; 
    }

    else if(business_datas.avg_rating ==2.0)
    {
    CGRect myImage1 =CGRectMake(190,5,20,22);
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
    [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView1];
    [imageView1 release]; 
    CGRect myImage2 =CGRectMake(210,5,20,22);
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
    [imageView2 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView2];
    [imageView2 release]; 
    CGRect myImage3 =CGRectMake(230,5,20,22);
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
    [imageView3 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView3];
    [imageView3 release]; 
    CGRect myImage4 =CGRectMake(250,5,20,22);
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
    [imageView4 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView4];
    [imageView4 release]; 
    CGRect myImage5 =CGRectMake(270,5,20,22);
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
    [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView5];
    [imageView5 release]; 
}
else if(business_datas.avg_rating ==2.5)
{
    CGRect myImage1 =CGRectMake(190,5,20,22);
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
    [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView1];
    [imageView1 release]; 
    CGRect myImage2 =CGRectMake(210,5,20,22);
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
    [imageView2 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView2];
    [imageView2 release]; 
    CGRect myImage3 =CGRectMake(230,5,20,22);
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
    [imageView3 setImage:[UIImage imageNamed:@"Black-Half.png"]];
    [cell addSubview:imageView3];
    [imageView3 release]; 
    CGRect myImage4 =CGRectMake(250,5,20,22);
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
    [imageView4 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView4];
    [imageView4 release]; 
    CGRect myImage5 =CGRectMake(270,5,20,22);
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
    [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView5];
    [imageView5 release]; 

}

else if(business_datas.avg_rating ==3.0)
{
    CGRect myImage1 =CGRectMake(190,5,20,22);
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
    [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView1];
    [imageView1 release]; 
    CGRect myImage2 =CGRectMake(210,5,20,22);
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
    [imageView2 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView2];
    [imageView2 release]; 
    CGRect myImage3 =CGRectMake(230,5,20,22);
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
    [imageView3 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView3];
    [imageView3 release]; 
    CGRect myImage4 =CGRectMake(250,5   ,20,22);
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
    [imageView4 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView4];
    [imageView4 release]; 
    CGRect myImage5 =CGRectMake(270,5,20,22);
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
    [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView5];
    [imageView5 release]; 

}
else if(business_datas.avg_rating ==3.5)
{
    CGRect myImage1 =CGRectMake(190,5,20,22);
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
    [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView1];
    [imageView1 release]; 
    CGRect myImage2 =CGRectMake(210,5,20,22);
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
    [imageView2 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView2];
    [imageView2 release]; 
    CGRect myImage3 =CGRectMake(230,5,20,22);
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
    [imageView3 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView3];
    [imageView3 release]; 
    CGRect myImage4 =CGRectMake(250,5,20,22);
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
    [imageView4 setImage:[UIImage imageNamed:@"Black-Half.png"]];
    [cell addSubview:imageView4];
    [imageView4 release]; 
    CGRect myImage5 =CGRectMake(270,5,20,22);
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
    [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView5];
    [imageView5 release]; 

}
else if(business_datas.avg_rating ==4.0)
{
    CGRect myImage1 =CGRectMake(190,5,20,22);
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
    [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView1];
    [imageView1 release]; 
    CGRect myImage2 =CGRectMake(210,5,20,22);
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
    [imageView2 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView2];
    [imageView2 release]; 
    CGRect myImage3 =CGRectMake(230,5,20,22);
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
    [imageView3 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView3];
    [imageView3 release]; 
    CGRect myImage4 =CGRectMake(250,5,20,22);
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
    [imageView4 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView4];
    [imageView4 release]; 
    CGRect myImage5 =CGRectMake(270,5,20,22);
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
    [imageView5 setImage:[UIImage imageNamed:@"emStar.png"]];
    [cell addSubview:imageView5];
    [imageView5 release]; 

}
else if(business_datas.avg_rating ==4.5)
{
    CGRect myImage1 =CGRectMake(190,5,20,22);
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
    [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView1];
    [imageView1 release]; 
    CGRect myImage2 =CGRectMake(210,5,20,22);
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
    [imageView2 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView2];
    [imageView2 release]; 
    CGRect myImage3 =CGRectMake(230,5,20,22);
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
    [imageView3 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView3];
    [imageView3 release];       
    CGRect myImage4 =CGRectMake(250,5,20,22);
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
    [imageView4 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView4];
    [imageView4 release]; 
    CGRect myImage5 =CGRectMake(270,5,20,22);
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
    [imageView5 setImage:[UIImage imageNamed:@"Black-Half.png"]];
    [cell addSubview:imageView5];
    [imageView5 release]; 
}

else //if(business_datas.avg_rating ==5.0)
{
    CGRect myImage1 =CGRectMake(190,5,20,22);
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:myImage1];
    [imageView1 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView1];
    [imageView1 release]; 
    CGRect myImage2 =CGRectMake(210,5,20,22);
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:myImage2];
    [imageView2 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView2];
    [imageView2 release]; 
    CGRect myImage3 =CGRectMake(230,5,20,22);
    UIImageView *imageView3 = [[UIImageView alloc] initWithFrame:myImage3];
    [imageView3 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView3];
    [imageView3 release]; 
    CGRect myImage4 =CGRectMake(250,5,20,22);
    UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:myImage4];
    [imageView4 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView4];
    [imageView4 release]; 
    CGRect myImage5 =CGRectMake(270,5 ,20,22);
    UIImageView *imageView5 = [[UIImageView alloc] initWithFrame:myImage5];
    [imageView5 setImage:[UIImage imageNamed:@"star.png"]];
    [cell addSubview:imageView5];
    [imageView5 release]; 
}







return cell;

}

当编译器出现该代码然后给出以下错误...

  

2011-09-13 12:24:13.641 Yelp-OnTheWay [8739:207] *断言失败 - [UITableView _createPreparedCellForGlobalRow:withIndexPath:],/ SourceCache / UIKit_Sim / UIKit-1447.6.4 / UITableView。 L:5613   2011-09-13 12:24:13.643 Yelp-OnTheWay [8739:207] 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView dataSource必须返回一个来自tableView的单元格:cellForRowAtIndexPath:'   * *第一次投掷时调用堆栈:   (       0 CoreFoundation 0x012bebe9 exceptionPreprocess + 185       1 libobjc.A.dylib 0x014135c2 objc_exception_throw + 47       2 CoreFoundation 0x01277628 + [NSException raise:format:arguments:] + 136       3基金会0x0016747b - [NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116       4 UIKit 0x003e08f3 - [UITableView(UITableViewInternal)_createPreparedCellForGlobalRow:withIndexPath:] + 883       5 UIKit 0x003d677f - [UITableView(UITableViewInternal)_createPreparedCellForGlobalRow:] + 75       6 UIKit 0x003eb450 - [UITableView(_UITableViewPrivate)_updateVisibleCellsNow:] + 1561       7 UIKit 0x003e3538 - [UITableView layoutSubviews] + 242       8 QuartzCore 0x00fca451 - [CALayer layoutSublayers] + 181       9 QuartzCore 0x00fca17c CALayerLayoutIfNeeded + 220       10 QuartzCore 0x00fc337c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310       11 QuartzCore 0x00fc30d0 _ZN2CA11Transaction6commitEv + 292       12 QuartzCore 0x00ff37d5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99       13 CoreFoundation 0x0129ffbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 27       14 CoreFoundation 0x012350e7 __CFRunLoopDoObservers + 295       15 CoreFoundation 0x011fdbd7 __CFRunLoopRun + 1575       16 CoreFoundation 0x011fd240 CFRunLoopRunSpecific + 208       17 CoreFoundation 0x011fd161 CFRunLoopRunInMode + 97       18 GraphicsServices 0x01b60268 GSEventRunModal + 217       19 GraphicsServices 0x01b6032d GSEventRun + 115       20 UIKit 0x0037b42e UIApplicationMain + 1160       21 Yelp-OnTheWay 0x00002154 main + 102       22 Yelp-OnTheWay 0x000020e5开始+ 53   )   抛出'NSException'实例后终止调用   程序收到信号:“SIGABRT”。   杀   退出

如何删除它?

提前致谢...

1 个答案:

答案 0 :(得分:1)

如果您使用两种不同类型的单元格,则需要使用两个重用标识符。

目前尚不清楚customCell是什么或什么时候会被设置为什么。

您不应该一直实例化并添加新的图像视图。这将导致重新使用的单元具有彼此叠加的多个图像视图。首次实例化单元格时应添加图像视图,并在重新使用单元格时进行配置。虽然看起来您使用5个图像视图来显示星级,但您可以使用unicode明星字符来实现此文本。

您的代码崩溃了,因为您要访问代码的//Configure the cell...部分,此时cell为零,然后您返回nil,导致异常提升,因为你必须返回一个UITableViewCell对象。

研究文档中的单元重用函数并在那里应用模式。这很简单:

  • 根据您的indexPath
  • 确定重用标识符
  • 尝试使用该标识符将单元格出列
  • 如果没有返回任何内容,请创建单元格或从笔尖加载单元格,此时创建任何子视图
  • 配置您出列或新创建的单元格的单元格和子视图
  • 返回已完成的单元格