0

I have created a custom UITableViewCell with three UILabel and one UIImageView , 1:I created sub class of UITableViewCell , 2:Added the Three Labels and one UIImageView , also in the xib file 3:Creating the cell using following way in cellForRowAtIndexpath

static NSString *CellIdentifier = @"NarrativeCell";

NarrativeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil){
    NSLog(@"New Cell Made");

    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NarrativeCell" owner:nil options:nil];

    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[NarrativeCell class]])
        {
            cell = (NarrativeCell *)currentObject;
            break;
        }
    }
}

Narrative n=(Narrative*)[tableArray objectAtIndexPath:indexpath.row];

[cell.articleName setText:n.title];

NSData *data=[[NSData alloc]initWithContentsOfFile:n.file]
[cell.sideImageView setImage:[UIImage imageWithData:data]];

problem is this , its always created new cell and i am getting memory warning and crash after adding few images . please help how can reuse the cell in above code so that it should not use much memory

Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
Ali
  • 10,774
  • 10
  • 56
  • 83
  • dupe x 2 http://stackoverflow.com/questions/540345/how-do-you-load-custom-uitableviewcells-from-xib-files http://stackoverflow.com/questions/413993/loading-a-reusable-uitableviewcell-from-a-nib – Daij-Djan Dec 06 '12 at 14:25

1 Answers1

0

Your code is fine. You just never set the reusable identifier of the cell so they are not reused (as you noticed).

In you custom cell class, implement the method reuseIdentifier:

- (NSString *) reuseIdentifier {
    return @"NarrativeCell";
}

or set the identifier in interface builder (in the inspector for the cell).

Pang
  • 9,564
  • 146
  • 81
  • 122
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135