简体   繁体   中英

how have custom UITableViewCell from nib which can be reused

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM