-1

我正在使用表格视图,其中我通过自定义单元格显示 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.m:5613 2011-09 -13 12:24:13.643 Yelp-OnTheWay[8739:207]由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'UITableView dataSource 必须从 tableView 返回一个单元格:cellForRowAtIndexPath:' * * 第一次抛出调用堆栈:(0 CoreFoundation 0x012bebe9exceptionPreprocess + 185 1 libobjc.A.dylib 0x014135c2 objc_exception_throw + 47 2 CoreFoundation 0x01277628 +[NSException raise:format:arguments:] + 136 3 Foundation 0x0016747b -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 4 UIKit 0x -[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_FUNCTION0x00fca17c 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_FUNCTION0x00fca17c 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_FUNCTION13 核心基础 0x0129ffbb __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION13 核心基础 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 start + 53 ) 在抛出“NSException”实例后调用终止程序接收信号:“SIGABRT”。杀死退出

怎么去掉?

提前致谢...

4

1 回答 1

1

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

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

您不应该一直实例化和添加新的图像视图。这将导致重复使用的单元格在彼此之上具有多个图像视图。图像视图应在您第一次实例化单元时添加,并在您重用它时进行配置。尽管看起来您正在使用 5 个图像视图来显示星级,但您可以使用我认为使用 unicode 星号的文本来实现这一点。

您的代码正在崩溃,因为您正在进入//Configure the cell...代码部分,此时cell为 nil,然后您返回nil导致引发异常,因为您必须返回一个UITableViewCell对象。

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

  • 根据您的确定重用标识符indexPath
  • 尝试使具有该标识符的单元出列
  • 如果没有返回任何内容,则创建单元格或从笔尖加载它,此时创建任何子视图
  • 配置出列或新创建的单元格的单元格和任何子视图
  • 返回完成的单元格
于 2011-09-13T07:22:52.103 回答