我有一个应用程序,其中包含一个带有多个表格视图的滚动视图。每个表视图都是从另一个视图控制器加载的。它是从 Apple 的 PageControl 示例应用程序构建的。我的目标是让它像苹果的天气应用一样工作。
我的一切工作都很好。所有内容都完美加载,左右滚动显示所有正确的表格视图及其相关数据。我有一个按钮,单击它会打开另一个视图,允许您编辑每个项目,就像天气应用程序一样,您可以在其中添加新城市、删除它或移动它。
我遇到的问题是当用户完成编辑项目时如何更新滚动视图。想象一下,在 PageControl 应用程序中能够删除第 5 页或将第 4 页移动到位置 #1,等等。
我没有粘贴任何代码,因为:1)它与 PageControl 应用程序中的代码相同;2)我还没想好从哪里开始。希望这里有人可以帮助我。
谢谢。
更新:(2010 年 3 月 5 日美国东部标准时间上午 3:18)
好的,所以我已经为此工作了一段时间。我能够在 mainView 中调用一个方法来更新他的滚动视图。代码看起来有点笨拙,但它确实有效!我真的不喜欢该代码,因为我最终使用了一次属性,并且我无法再次设置它,因为我得到一个错误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;
}
然后我有我的代码来刷新滚动视图。我所做的是删除所有子视图并再次加载它们。我永远不会有超过 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
方法从子视图中调用,其中可以添加或删除要在滚动视图上显示的项目。