我需要帮助,在UIViewController
TableView
实施一些UIPageViewController
这是我目前的代码,但不起作用。出现了一些UIViewController
,但TableView
为空,没有单元格。
APageViewController
- (void)viewDidLoad {
[super viewDidLoad];
pages = [NSMutableArray array];
self.delegate = self;
self.dataSource = self;
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self setupPageViewController];
}
- (void)setupPageViewController{
for (int i = 0; i < items.count; i++) {
AViewControllerWithTableView *detail = (AViewControllerWithTableView *)[self.storyboard instantiateViewControllerWithIdentifier:@"VCID"];
[detail reloadTableView:items[i]]; // tableview reload data here
[pages addObject:detail];
}
[self setViewControllers:@[pages[0]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSInteger currentIndex = [pages indexOfObject:viewController];
NSInteger previousIndex = currentIndex-1;
if (previousIndex < 0) {
return nil;
}
return pages[previousIndex];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSInteger currentIndex = [pages indexOfObject:viewController];
NSInteger nextIndex = currentIndex+1;
if (nextIndex > pages.count - 1) {
return nil;
}
return pages[nextIndex];
}
AViewControllerWithTableView
@implementation AViewControllerWithTableView{
NSMutableArray *details;
}
- (void)viewDidLoad {
[super viewDidLoad];
details = [NSMutableArray array];
}
- (void)reloadTableView:(NSDictionary *)detail{
if([detail.type isEqualToString:@"A"]){
[details addObject:something];
} else if([detail.type isEqualToString:@"B"]){
[details addObject:something];
} else if([detail.type isEqualToString:@"C"]){
[details addObject:something];
} else {
[details addObject:something];
}
[self.detailTableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"details: %@", details); // details always empty / @[]
return details.count;
}
tableview为空,没有单元格。
如何实施此案例?
答案 0 :(得分:0)
问题:
您正在设置
details = [NSMutableArray array];
在tableView控制器的viewDidLoad中,一旦PageViewController加载你的tableView,就会调用它。
但是,即使在加载tableView之前,也是通过调用[detail reloadTableView:items[i]];
将数据发送到tableView控制器。
所以你要么叫reloadTableView
,要么细节总是零
或者即使它已填充,您在viewDidLoad中的语句details = [NSMutableArray array];
现已清除它:)
解决方案:
for (int i = 0; i < items.count; i++) {
AViewControllerWithTableView *detail = (AViewControllerWithTableView *)[self.storyboard instantiateViewControllerWithIdentifier:@"VCID"];
if(detail.details == nil) {
detail.details = [NSMutableArray array];
}
[detail.details.addObject:items[i]];
[pages addObject:detail];
}
最后 从tableView
的viewDidLoad中删除details = [NSMutableArray array];