Search code examples
iphoneobjective-ccocoa-touchuitableviewios4

iPhone app crashes after execution of cellForRowAtIndexPath method of tableView


App crashes after execution of cellForRowAtIndexPath method of tableView

It goes into:

UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:]

and crashes out.

There is no error shown in Console. It shows EXC_BAD_EXCESS in Status bar of Xcode.

What could be wrong?

EDIT 1:

This is the whole code in my tableView's cellForRowAtIndexPath method:

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

static NSString *CellIdentifier = @"Cell";
static NSString *newCellIdentifier = @"NewCell";

UITableViewCell *cell = nil;
NSUInteger row = [indexPath row];
NSUInteger count;
if (searching==YES) {
    count = [searchCellTextArray count];
}else {
    count = [cellTextArray count];
} 

if(row==count)
      {

    cell = [tableView dequeueReusableCellWithIdentifier:newCellIdentifier];
    if (cell == nil) 
               {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:newCellIdentifier] autorelease];
    }
    cell.textLabel.text = @"Load more items...";


    cell.textLabel.textColor = [UIColor blueColor];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

}
else 
      {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
              {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }


    UIImage *cellImage = [[UIImage alloc] init];
    NSString *imgName;

    //Searching


    if(searching==YES && [searchCellTextArray count] != 0) 
               {

        NSString *decodedImageName = [NSString stringWithUTF8String:[[[showSearchImageArray objectAtIndex:indexPath.row]valueForKey:@"image"] cStringUsingEncoding:[NSString defaultCStringEncoding]]];
        imgName = decodedImageName;

        cell.textLabel.textColor = [UIColor redColor];
        cell.textLabel.text=[searchCellTextArray objectAtIndex:indexPath.row];

        cell.detailTextLabel.font= [UIFont boldSystemFontOfSize:18];
        cell.detailTextLabel.textColor = [UIColor blackColor];
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.detailTextLabel.numberOfLines= [lableNameArray count]+2;
        cell.detailTextLabel.text = [searchDetailTextArray objectAtIndex:indexPath.row];
    }
    else
              { //searching 

        NSString *decodedImageName = [NSString stringWithUTF8String:[[[showImageArray objectAtIndex:indexPath.row] valueForKey:@"image"] cStringUsingEncoding:[NSString defaultCStringEncoding]]];

        imgName = decodedImageName;


        cell.textLabel.textColor= [UIColor redColor];
        cell.textLabel.text = [cellTextArray objectAtIndex:indexPath.row];



        cell.detailTextLabel.font= [UIFont boldSystemFontOfSize:18];
        cell.detailTextLabel.textColor = [UIColor blackColor];
        cell.detailTextLabel.numberOfLines= [lableNameArray count]+2;
        cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.detailTextLabel.text = [detailTextArray objectAtIndex:indexPath.row];

    }

    if (imgName !=(NSString*)[NSNull null] && ![imgName isEqualToString:@""] && ![imgName isEqualToString:@"X"]) 
              {
        NSLog(@" Image Name : %@",imgName);

        NSString *documentsDirectory = [self getImagePath];
        NSError *error1;
        NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error1];
        if (files == nil) 
                        {
            NSLog(@"Error reading contents of documents directory: %@", [error1 localizedDescription]);
        }


        for (NSString *file in files) 
                        {
            NSLog(@"Loop Entered");
            if([file isEqualToString:[NSString stringWithFormat:@"%@_thumb.png",imgName]])
                  {
                NSLog(@"Image: %@ %@",file,imgName);
                NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:file];

                cellImage = [UIImage imageWithContentsOfFile:fullPath];
                NSLog(@"Full Path: %@",fullPath);

            }

        }

        cell.imageView.image = cellImage;
    }
              else 
              {
        cell.imageView.image = [UIImage imageNamed:@"GlossaryGhostImg1.png"];
    }

}
NSLog(@"Cell For Row At Index Path Done");
return cell;
}

Are there memory leaks here, and have I over-released any objects?


Solution

  • Could be many things: You return a garbage cell. You released your cell too many times. You autoreleased some component of your cell too many times. Sounds like a memory management issue, check your retain/releases carefully. And post your cellForRowAtIndexPath code.