开发者

slow scrolling of UITableView [duplicate]

开发者 https://www.devze.com 2023-03-13 22:51 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: UIImage in uitableViewcell slowdowns scrolling table
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

UIImage in uitableViewcell slowdowns scrolling table

I filled my table view data with plist file which will download from a se开发者_如何学Crver . 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


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.


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;

}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号