Milad
Milad

Reputation: 1269

UITableView is slow

I have a view which I dragged a UITableView in it, and 2 UIImageViews (the 1st one to show a background image and the second one is just showing a very small title at the top of the view with an image). They're all set to weak properties. The tableView has 495 rows and an image loads into each cell. Here's the code for configuring a cell:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }    

    int cellNumber = indexPath.row + 1;
    NSString *cellImage1 = [NSString stringWithFormat:@"c%i.png", cellNumber];
    UIImage *theImage = [UIImage imageNamed:cellImage1];

    [cell.imageView setImage:theImage];


    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor brownColor]];
    [cell setSelectedBackgroundView:bgColorView];

    return cell;
}

I use segues, so I don't need the delegate method. (which not relevant to this question either). The thing is, the scrolling is smooth; BUT if I scroll TOO FAST then it gets slow and probably causes the app to crash. The png images loaded into cells are not big sizes, each one is around 5KB. It seems the the dequeueing job is not done efficiently; cuz it gets slow, shows a memory warning message and then the crash happens. Anything I can do about it? (there's nothing else in the view)

UPDATE: I've also tried removing this part of code:

UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor brownColor]];
[cell setSelectedBackgroundView:bgColorView];

no use!

Upvotes: 2

Views: 372

Answers (2)

Michel Müller
Michel Müller

Reputation: 5695

Try releasing the image after the setImage command. Does this help?

Upvotes: 0

joshOfAllTrades
joshOfAllTrades

Reputation: 1982

UIImage imageNamed caches your images and never frees the memory which is probably the cause of your memory warnings and crash. When dealing with large number of images it usually a good idea to load your images in some other way, such as imageWithContentsOfFile or imageWithData.

Upvotes: 4

Related Questions