开发者

Updating a ScrollView's Content

I have an app which contains a scrollview with several tableviews. Each tableview is loaded from another viewcontroller. It is built from the PageControl sample app from Apple. My goal is to have it work exactly like Apple's weather app.

I have everything working just fine. All content loads perfectly and scrolling side to side displays all the proper tableviews and their relative data. I have a button which when clicked it opens up another view which allows you to edit each item, just like the weather app where you can add a new city, delete it, or move it around.

The problem I am having is how do I update the scrollview when the user is done editing the items. Imagine in the PageControl app being able to remove page 5 or move page 4 into position #1, etc.

I haven't pasted any code because: 1) it's the same code in the PageControl app; 2) I haven't figured out where to start. Hopefully someone here can help me.

Thanks.

UPDATE: (3/5/2010 3:18AM EST)

Ok, so I been working on this for a while. I was able to call a method in the mainView to update he scrollView. The code seems kinda clunky, but it works! I really do not like the code because I end up using a property once, and I cannot set it again, because I get an error objc[10801]: FREED(id): message release sent to freed object=0x3f4a490. To me it seems like the object has been release? If so, I don't know how, as I only release it in the dealloc. dealloc is never called (I put an NSLog to check) so I don't know what's going on.

Code: The initial code is the same code from PageControl sample app from Apple:

- (void)viewDidLoad {
    [super viewDidLoad];
    appDel = (iBarryAppDelegate *)[[UIApplication sharedApplication] delegate];
    managedObjectContext = appDel.managedObjectContext;
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < numberOfPages; i++)
    {
        [controllers addObject:[NSNull null]];
    }
    self.viewControllers = controllers;
    [controllers release];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfPages, scrollView.frame.size.height);
    scrollView.showsHori开发者_StackOverflow社区zontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;
    pageControl.numberOfPages = numberOfPages;
    pageControl.currentPage = 0;
    //I end up commenting out all the method calls to
    //[self loadScrollViewWithPage:xxx]; and load all the views at once.
    //I know I should be lazy-loading here , but Scrolling is much 
    //faster if I load them all at once, instead 
    //of when they are about to be viewed.
    for (unsigned i = 0; i < numberOfPages; i++)
    {
        [self loadScrollViewWithPage:i];
    }
}
- (void)loadMainController:(int)page
{
    MainViewController *controller = [viewControllers objectAtIndex:page];
    if ((NSNull *)controller == [NSNull null])
    {
        controller = [[MainViewController alloc] initWithPageNumber:page];
        [viewControllers replaceObjectAtIndex:page withObject:controller];
        [controller release];
    }
    if (nil == controller.view.superview)
    {
        CGRect frame = scrollView.frame;
        frame.origin.x = frame.size.width * page;
        frame.origin.y = 0;
        controller.view.frame = frame;
        [scrollView addSubview:controller.view];
    }
}
- (void)loadScrollViewWithPage:(int)page
{
    if (page < 0) return;
    if (page >= numberOfPages) return;
    [self loadMainController:page];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    if (pageControlUsed)
    {
        return;
    }
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
//  [self loadScrollViewWithPage:page - 1];
//  [self loadScrollViewWithPage:page];
//  [self loadScrollViewWithPage:page + 1];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
    int page = pageControl.currentPage;
//  [self loadScrollViewWithPage:page - 1];
//  [self loadScrollViewWithPage:page];
//  [self loadScrollViewWithPage:page + 1];
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [scrollView scrollRectToVisible:frame animated:YES];
    pageControlUsed = YES;
}

I then have my code to refresh the scrollView. What I do is remove all the subviews and load them again. I won't ever have more than 13 subViews, not sure if deleting them all and recreating them is a good or bad idea.

- (void)reloadDataNow
{
    for(UIView *subview in [scrollView subviews]) {
            [subview removeFromSuperview];
            NSLog(@"removed a suview");
    }
    [self loadData]; //this method fetches which data will be 
    //in the scrollView. It reloads the number of pages based 
    //on the return result and an NSMutableArray where I store 
    //data to pass to the views that will appear in the scrollView
    NSMutableArray *controllers = [[NSMutableArray alloc] init];
    for (unsigned i = 0; i < numberOfPages; i++)
    {
        [controllers addObject:[NSNull null]];
    }
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfPages, scrollView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.scrollsToTop = NO;
    scrollView.delegate = self;
    pageControl.numberOfPages = numberOfPages;
    pageControl.currentPage = 0;
    for (unsigned i = 0; i < numberOfPages; i++)
    {
        MainViewController *controller = [controllers objectAtIndex:i];
        if ((NSNull *)controller == [NSNull null])
        {
            controller = [[MainViewController alloc] initWithPageNumber:i team:[[favoritesArray objectAtIndex:i] teamR]];
            [controllers replaceObjectAtIndex:i withObject:controller];
            [controller release];
        }
        if (nil == controller.view.superview)
        {
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * i;
            frame.origin.y = 0;
            controller.view.frame = frame;
            [scrollView addSubview:controller.view];
        }
    }
    //********************CRASH******************
    //Ideally, I think, the line below would replace the contents of
    //viewControllers with the new controllers. Here is where I get the 
    //"objc[10801]: FREED(id): message release sent to freed object=0x3f4a490" 
    //error. This line should probably be placed after [self loadData] or something,
    //but it crashes so I put but here to display better. I tried
    //adding self.viewControllers = nil; but I don't think that is 
    //the problem with the error message.
    self.viewControllers = controllers;
}

The reloadDataNow method is called from a child view where one can add or remove items to show on the scrollView.


(I have had the same problem, I wanted to add UIImageView's to my mainView and update them like that) I just used an NSMutable array of the ViewControllers you want to display in the view, then add or subtract from that as needed, and every time you add or subtract from that array, then call a metod from inside your mainView that goes through the scrollView and removes every view from the scrollView, and then adds the appropriate viewController to the scrollView. Hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜