我有一个应用程序,其中包含一个包含多个tableviews的scrollview。每个tableview都从另一个viewcontroller加载。它是从Apple的PageControl示例应用程序构建的。我的目标是让它像Apple的天气应用程序一样工作。
我把一切都运转得很好。所有内容都完美加载,并且左右滚动显示所有正确的tableviews及其相关数据。我有一个按钮,当点击它时会打开另一个视图,允许您编辑每个项目,就像天气应用程序一样,您可以添加新城市,删除它或移动它。
我遇到的问题是当用户完成编辑项目时如何更新滚动视图。想象一下,PageControl应用程序能够删除第5页或将第4页移动到位置#1等。
我没有粘贴任何代码,因为:1)它与PageControl应用程序中的代码相同; 2)我还没弄明白从哪里开始。希望有人可以帮助我。
感谢。
更新:(2010年3月5日美国东部时间上午3:18)
好的,所以我一直在研究这个问题。我能够在mainView中调用一个方法来更新scrollView。代码似乎有点笨重,但它的工作原理!我真的不喜欢这些代码,因为我最终使用了一次属性,而我无法再次设置它,因为我收到错误objc[10801]: FREED(id): message release sent to freed object=0x3f4a490
。对我来说,似乎对象已被释放?如果是这样,我不知道如何,因为我只在dealloc
中发布它。永远不会调用dealloc
(我会检查NSLog
)所以我不知道发生了什么。
代码:初始代码与Apple的PageControl示例应用程序中的代码相同:
- (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.showsHorizontalScrollIndicator = 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;
}
然后我将代码添加到 刷新 scrollView。我所做的是删除所有子视图并重新加载它们。我不会有超过13个子视图,不确定是否删除它们并重新创建它们是一个好主意或坏主意。
- (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;
}
从子视图调用reloadDataNow
方法,可以添加或删除要在scrollView上显示的项。
答案 0 :(得分:2)
(我遇到了同样的问题,我想将UIImageView
添加到我的mainView并更新它们)我只是使用了想要在视图中显示的ViewControllers的NSMutable数组,然后添加或者根据需要从中减去,并且每次从该数组中添加或减去,然后从mainView中调用通过scrollView的metod并从scrollView中删除每个视图,然后将相应的viewController添加到scrollView。希望这会有所帮助。