1

我已经阅读有关堆栈溢出的内容已经有一段时间了,但这是我的第一篇文章,只是因为这是我第一次遇到其他人似乎还没有解决的问题!

好吧,言归正传。将 UITableViews 放在 UIPageView 中应该是一件简单的事情,但我遇到了困难。我有一个 ViewController 和 contentViewController。我正在使用 .xibs 而不是故事板。contentViewController.xib 是一个表视图,而 ViewController.xib 是一个视图。我只专注于 iPhone。UITableView 连接到名为 theTableView 的 dataSource、delegate 和引用 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{} 之后崩溃,我还没有学会如何修复自动布局/ layoutSubview 错误。还有人知道怎么做吗?

我在 ios 开发方面的经验有限,所以我确信我只是在正确的位置没有正确的部分。我使用http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_UIPageViewController_Application来做到这一点。

我的代码如下:

视图控制器.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

视图控制器.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

内容视图控制器.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

内容视图控制器.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

因此,如果有人能理顺我,我将不胜感激。

4

1 回答 1

1

我能够解决自己的问题,但似乎我有点搞混了。

rdelmar 的评论让我走上了正确的道路,但我将委托和数据源连接到了错误的对象。我必须将它们连接到 File's Owner 才能使其工作。

此外,似乎没有必要使用 TableView,当我删除时,我的代码突然按预期工作。

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

于 2013-03-17T23:05:12.107 回答