开发者

Reuse cells of a scroll view

开发者 https://www.devze.com 2023-02-24 00:50 出处:网络
I am using a scrollview to display various items in a horizontal fashion. How can I \"reuse the cells\" to conserve memory:

I am using a scrollview to display various items in a horizontal fashion.

How can I "reuse the cells" to conserve memory:

Edit: Apologies for not being clear. I plan to place a few labels and and another ImageView inside each ImageView so I am keen to reuse this each time instead of instantiating new items each time. e.g. change the image's and update the labels instead of re-create it.

// Implement viewDidLoad to do additional setup after loading the view, typic开发者_如何转开发ally from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    // load all the images from our bundle and add them to the scroll view
//  NSUInteger i;
    for (int i = 0; i <= 150; i++)
    {
        NSString *imageName = @"tempImage.jpg";
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
        CGRect rect = imageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [self.bestSellScrollView addSubview:imageView];
        [imageView release];
    }

    [self layoutScrollImages];  // now place the photos in serial layout within the scrollview
}

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [self.bestSellScrollView subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [self.bestSellScrollView setContentSize:CGSizeMake((150 * kScrollObjWidth), 
                                                       [self.bestSellScrollView bounds].size.height)];
}


If you mean like what happens with respect UITableViewCell in a UITableView, then its not possible in the horizontal scroller.

The reusable cells property for a table view is provided by the system and it not applicable for horizontal scroller.

You have to write on your own logic in order to achieve it, perhaps! Its a good question though. Will be tuned to other answers just in case if anything is possible.


If you mean you'd like to keep the UIImageView(s) where they are in the scrollview, but you'd like to change the UIImage that they show, try something like this:

-(void)changeImageViewWithTag:(NSInteger)aTag toImage:(UIImage *)anImage
{
     UIImageView *theView = [self.bestSellScrollView viewWithTag:aTag];
     if (theView)
     {
         [theView setImage:anImage];
     }
}


I am assuming that you intend to reuse the UIImageView cell instances in the UIScrollView. I hope that Daniel Tull's implementation may be helpful--

https://github.com/danielctull/DTGridView

You may like to see an Apple sample code on UIScrollView as well(I don't have the link, but u can easily find it on the web)

Thanks! Ishank

0

精彩评论

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

关注公众号