在每个页面中创建一个带UITableView的UIPageViewController

时间:2013-03-17 02:53:53

标签: objective-c xcode uitableview uipageviewcontroller

我已经在堆栈溢出上阅读了很长一段时间,但这是我的第一篇文章,只是因为这是我第一次遇到其他人似乎没有修复过的问题!

好的,直到生意。将UITableViews放在UIPageView中应该是一件简单的事情,但我遇到了困难。我有一个ViewController和contentViewController。我使用.xibs而不是故事板。 contentViewController.xib是一个Table View,ViewController.xib是一个View。我只专注于iPhone。 UITableView连接到名为theTableView的dataSource,delegate和Referencing Outlet。

项目构建但是当我运行它时,我收到以下错误消息:

    2013-03-17 16:14:23.026 pageApp[775:c07] *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2380.17/UIView.m:5776
    2013-03-17 16:14:23.028 pageApp[775:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
    *** First throw call stack:
    (0x1c93012 0x10d0e7e 0x1c92e78 0xb66665 0x6539f 0x10e46b0 0x228ffc0 0x228433c 0x228feaf 0x1042bd 0x4cb56 0x4b66f 0x4b589 0x4a7e4 0x4a61e 0x4b3d9 0x4e2d2 0xf899c 0x45574 0x4576f 0x45905 0x4e917 0x20eb 0x12157 0x12747 0x1394b 0x24cb5 0x25beb 0x17698 0x1beedf9 0x1beead0 0x1c08bf5 0x1c08962 0x1c39bb6 0x1c38f44 0x1c38e1b 0x1317a 0x14ffc 0x1d2d 0x1c55)
    libc++abi.dylib: terminate called throwing an exception

这在ViewController.m中的 - (void)viewDidLoad {}之后崩溃,我还没有学习如何修复auto layout / layoutSubview错误。还有其他人知道吗?

我对ios开发的经验有限,所以我确信我在正确的位置上没有合适的部分。我使用http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application来实现这一目标。

我的代码如下:

ViewController.h

    #import <UIKit/UIKit.h>
    #import "contentViewController.h"

    @interface ViewController : UIViewController
    <UIPageViewControllerDataSource>
    {
        UIPageViewController *pageController;
        NSArray *pageContent;
    }
    @property (strong, nonatomic) UIPageViewController *pageController;
    @property (strong, nonatomic) NSArray *pageContent;
    @end

ViewController.m

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController
    @synthesize pageController, pageContent;

    - (contentViewController *)viewControllerAtIndex:(NSUInteger)index
    {
        // Return the data view controller for the given index.
        if (([self.pageContent count] == 0) || (index >= [self.pageContent count])) {
            return nil;
        }

        // Create a new view controller and pass suitable data.
        contentViewController *dataViewController =[[contentViewController alloc]initWithNibName:@"contentViewController"bundle:nil];
        dataViewController.dataObject =[self.pageContent objectAtIndex:index];
        return dataViewController;
    }

    - (NSUInteger)indexOfViewController:(contentViewController *)viewController
    {
        return [self.pageContent indexOfObject:viewController.dataObject];
    }

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
    {
        NSUInteger index = [self indexOfViewController:(contentViewController *)viewController];
        if ((index == 0) || (index == NSNotFound)) {
            return nil;
        }

        index--;
        return [self viewControllerAtIndex:index];
    }        

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
    {
        NSUInteger index = [self indexOfViewController:(contentViewController *)viewController];
        if (index == NSNotFound) {
            return nil;
        }

        index++;
        if (index == [self.pageContent count]) {
            return nil;
        }
        return [self viewControllerAtIndex:index];
    }

    - (void) createContentPages
    {
        NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
        for (int i = 1; i < 4; i++)
        {        
            NSString *contentString = [[NSString alloc]initWithFormat:@"Chapter %d \nThis is the page %d of content displayed using UIPageViewController in iOS 5.", i, i];
            [pageStrings addObject:contentString];
        }
        pageContent = [[NSArray alloc] initWithArray:pageStrings];
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self createContentPages];

        self.pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];

        pageController.dataSource = self;
        [[pageController view] setFrame:[[self view] bounds]];

        contentViewController *initialViewController = [self viewControllerAtIndex:0];
        NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];

        [pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

        [self addChildViewController:pageController];
        [[self view] addSubview:[pageController view]];
        [pageController didMoveToParentViewController:self];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end

contentViewController.h

    #import <UIKit/UIKit.h>

    @interface contentViewController : UIViewController
    @property (strong, nonatomic) IBOutlet UITableView *theTableView;
    @property (strong, nonatomic) id dataObject;
    @property (strong, nonatomic) NSArray *pageContent;
    @end

contentViewController.m

    #import "contentViewController.h"

    @interface contentViewController ()

    @end

    @implementation contentViewController
    @synthesize theTableView, dataObject, pageContent;

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    }
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

    - (void) createContentPages
    {
        NSMutableArray *pageStrings = [[NSMutableArray alloc] init];
        for (int i = 1; i < 4; i++)
        {
            NSString *contentString = [[NSString alloc]initWithFormat:@"Chapter %d \nThis is the page %d of content displayed using UIPageViewController in iOS 5.", i, i];
            [pageStrings addObject:contentString];
        }
        pageContent = [[NSArray alloc] initWithArray:pageStrings];
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self createContentPages];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection (NSInteger)section
    {
        return 4;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"SimpleTableItem";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [pageContent objectAtIndex:indexPath.row];
        return cell;
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end

所以如果有人能理顺我,我会很感激。

1 个答案:

答案 0 :(得分:1)

我能够解决自己的问题,看起来我只是有些混乱。

rdelmar的评论让我走上了正确的轨道,但我将委托和数据源挂钩到了错误的对象。我必须将它们连接到文件所有者才能使其正常工作。

此外,似乎theTableView不是必需的,当我删除时,我的代码突然按预期工作。

如果这还不够清楚,请告诉我如何更具体。谢谢!