2

Possible Duplicate:
UIImage in uitableViewcell slowdowns scrolling table

I filled my table view data with plist file which will download from a server . but after user scrolls the table the scrolling is very slow ! this is my code :

    //this is my plist code that load from server 
         NSURL *url = [NSURL URLWithString:@"http://example.com/news.plist"];
         titles = [[NSArray arrayWithContentsOfURL:url] retain];


tableview :

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return titles.count;
    return subtitle.count;
}




    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

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


        tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.gif"]];



        // Configure the cell.
        NSUInteger row = [indexPath row]; 
        cell.textLabel.text = [titles objectAtIndex:row];
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];


        cell.detailTextLabel.text = [subtitle objectAtIndex:row]; 
        cell.detailTextLabel.numberOfLines = 6;
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];


        //gradiant BG for cells 
        UIImage *image = [UIImage imageNamed:@"bg2.gif"];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.contentMode = UIViewContentModeScaleToFill;
        cell.backgroundView = imageView;
        [imageView release];


        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        tableView.separatorColor = [UIColor lightTextColor];


        return cell;

    }

Thank you for telling me what's is my problem

Community
  • 1
  • 1
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162

3 Answers3

3

The problem is most likely that you are loading images (e.g., bg2.gif or bg.gif) within tableView:cellForRowAtIndexPath:. I would first try loading these images in the constructor or a separate method of your table view controller, store them in an instance variable, and then re-use them when needed.

Tamás
  • 47,239
  • 12
  • 105
  • 124
2

Give this a go:

- (void) viewDidLoad {
   [super viewDidLoad];

   tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.gif"]];
   tableView.separatorColor = [UIColor lightTextColor];
} 


// Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell";

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

        // Do all your desgin settings here, not if the cell gets dequeued.
        cell.textLabel.textColor = [UIColor darkGrayColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];

        //gradiant BG for cells 
        UIImage *image = [UIImage imageNamed:@"bg2.gif"];
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.contentMode = UIViewContentModeScaleToFill;
        cell.backgroundView = imageView;
        [imageView release];

        cell.detailTextLabel.numberOfLines = 6;
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

   // Configure the cell.
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [titles objectAtIndex:row];
    cell.detailTextLabel.text = [subtitle objectAtIndex:row]; 

    return cell;

}
rckoenes
  • 69,092
  • 8
  • 134
  • 166
1

Remove all the function/method call of tableView in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, you should configure the tableView in the -viewDidLoad or -loadView.

To improve scrolling speed, I recommend you checkout this: http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/.

cxa
  • 4,238
  • 26
  • 40