开发者

How to avoid the repaint effect when using the UITableView?

开发者 https://www.devze.com 2023-02-09 14:05 出处:网络
I was debugging an issue with a view controller recently and noticed that each time I drag the view up or down it will repaint the entire contents of my UITableView (as it calls the cellForRowAtIndexP

I was debugging an issue with a view controller recently and noticed that each time I drag the view up or down it will repaint the entire contents of my UITableView (as it calls the cellForRowAtIndexPath method each time). Is it possible to use an in memory datasource or add another delegate to my view controller so it won't repaint each time?

I'm not modifying anything inside the cells when the user interacts with it so my data source would be static after the initial "viewDidLoad" is called.

- (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] autorelease];
    }

  if ([self.hats count] > 0) {
    //do some complex stuff in here that hurts each time we re-draw this ...
  }

    return cell;  
}

Thank you in advance开发者_JAVA技巧


so // complex stuff means adding UIViews.

I make an example for an UIImageView. Since you don't show the complex stuff you have to adopt it on your own.

your code looks like this:

- (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] autorelease];
    }

    if ([self.hats count] > 0) {
        UIImageView *imageView = [[UIImageView....];
        [cell.contentView addSubView:imageView];
        [imageView setImage:foo];
        [imageView release];
    }
    return cell;
}

refactor your code that it looks like this:

- (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] autorelease];
        UIImageView *imageView = [[UIImageView....];
        [cell.contentView addSubView:imageView];
        imageView.tag = 42;
        [imageView release];
    }

    if ([self.hats count] > 0) {
        UIImageView *imageView = [cell viewWithTag:42];
        [imageView setImage:foo];
    }
    return cell;
}

et voila, your tableview is responsive. Because you create the subviews exactly one time for each cell. And when the cell isn't used anymore and goes into the reuse bin the subviews stay with it.

And if you need 4 imageViews in one cell and 8 in another, you add 8 imageviews when you create the cell and give them a frame of CGRectZero and of course a different tag for each view.
If you need them you show them, if you don't need them you set the image to nil and the frame to zero.

0

精彩评论

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