What is error in this code so that it is crashing?

-1

I am using table view in which I am showing 10 row of table by custom cell and one is by normal cell. I have use this code...

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

}

When compiler come on that code then give following error...

2011-09-13 12:24:13.641 Yelp-OnTheWay[8739:207] * Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:5613 2011-09-13 12:24:13.643 Yelp-OnTheWay[8739:207] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' ** Call stack at first throw: ( 0 CoreFoundation 0x012bebe9 exceptionPreprocess + 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 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 start + 53 ) terminate called after throwing an instance of 'NSException' Program received signal: “SIGABRT”. kill quit

How remove it?

Thanks in advance...

iphone
uitableview
custom-cell
asked on Stack Overflow Sep 13, 2011 by iRam11 • edited Sep 13, 2011 by jrturton

1 Answer

1

If you are using two separate types of cell you need to use two reuse identifiers.

It is not clear what customCell is or when that would ever be set to anything.

You should not be instantiating and adding new image views all the time. This will result in a re-used cell having multiple image views on top of each other. The image views should be added when you first instatiate the cell and configured when you are reusing it. Though it looks like you are using 5 image views to show a star rating, you can achieve this with text I think using unicode star characters.

Your code is crashing because you are getting to the //Configure the cell... section of the code, at this point cell is nil, you are then returning nil which is causing the exception to be raised, as you have to return a UITableViewCell object.

Study the cell reuse functions in the documentation and apply the pattern there. It is quite straightforward:

  • Determine reuse identifier based on your indexPath
  • Attempt to dequeue a cell with that identifier
  • If nothing is returned, create the cell or load it from a nib, creating any subviews at this point
  • Configure the cell and any subviews of your dequeued or newly created cell
  • Return the completed cell
answered on Stack Overflow Sep 13, 2011 by jrturton

User contributions licensed under CC BY-SA 3.0