我有一组UITableViewControllers,我在超级控制器中使用这个协议方法添加到容器和superview中:
-(void)indexPathSelected:(NSIndexPath*)selectedIndex;
{
//This method is fired when the index path is selected in the content container. The content container
//could contain WobletTableViewController, WobletDetailViewController or the EventsTableViewController
for (UIViewController *childViewController in [self childViewControllers])
{
if ([childViewController isKindOfClass:[WobletTableViewController class]])
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
TableDetailViewController *rewardsTableVC = [sb instantiateViewControllerWithIdentifier:@"restaurantDetailsTable"];
//tvc.delegate=self;
//found container view controller
TableDetailViewController *restaurantsTableVC = (TableDetailViewController *)childViewController;
[restaurantsTableVC removeFromParentViewController];
[restaurantsTableVC.view removeFromSuperview];
rewardsTableVC.rewards=[(NSDictionary*)[self.restaurants objectAtIndex:selectedIndex.row] objectForKey:@"rewards"];
[self addChildViewController:rewardsTableVC];
[self.contentContainer addSubview:rewardsTableVC.view];
}
}
}
据我所知,这很好用。接下来,我不得不调整单元格的大小,所以我实现了这样的委托方法:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath*)indexPath
{
if (indexPath.section == 0) {
if (indexPath.row == 0) {
return 347;
}
}
return 71;
}
细胞也非常标准地创建:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(indexPath.row==0)
{
NSString *CellIdentifier = @"topDetailCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
assert([cell isMemberOfClass:[HeaderCell class]]);
}
else
{
NSString *CellIdentifier = @"rewardCell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
assert([cell isMemberOfClass:[RewardCell class]]);
}
return cell;
}
添加此方法后,嵌入的tableview会在滚动后自动滚动到顶部。在调整高度之前没有发生这种情况。怎么了?
答案 0 :(得分:1)
当我以编程方式创建表视图时,这里有一个简单的问题。由于它位于容器视图中,因此需要将其框架设置为容器视图的边界:
-(void)indexPathSelected:(NSIndexPath*)selectedIndex;
{
//This method is fired when the index path is selected in the content container. The content container
//could contain WobletTableViewController, WobletDetailViewController or the EventsTableViewController
for (UIViewController *childViewController in [self childViewControllers])
{
if ([childViewController isKindOfClass:[WobletTableViewController class]])
{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
TableDetailViewController *rewardsTableVC = [sb instantiateViewControllerWithIdentifier:@"restaurantDetailsTable"];
//tvc.delegate=self;
//found container view controller
TableDetailViewController *restaurantsTableVC = (TableDetailViewController *)childViewController;
rewardsTableVC.view.frame=self.contentContainer.bounds;
[restaurantsTableVC removeFromParentViewController];
[restaurantsTableVC.view removeFromSuperview];
rewardsTableVC.rewards=[(NSDictionary*)[self.restaurants objectAtIndex:selectedIndex.row] objectForKey:@"rewards"];
[self addChildViewController:rewardsTableVC];
[self.contentContainer addSubview:rewardsTableVC.view];
}
}
}