使用带有2个可切换视图和分段控件的导航

时间:2014-04-22 01:39:49

标签: ios objective-c cocoa-touch uiview uinavigationcontroller

我有一个UISegmentedControl和两个UICollectionViews

这个UISegmentedControl可以在我的两个集合视图之间切换。

看看这张图片。在UISegmentedControl的左侧,您会看到分段控件上的单个项目视图显示视图按钮,该按钮右侧是网格显示按钮。

网格显示按钮链接到_collectionView 单个文件显示按钮链接到_collectionView2

默认情况下,我的应用首先访问了_collectionView。可以说我点击了精简按钮。我的UINavigation将我带到另一页然后如果我点击该页面上的按钮,它会将我带回_collectionView页面。

现在让我说点击单个文件显示按钮,将我带到_collectionView2页面,如下图所示。如果我然后点击精简按钮,然后点击返回以带我回到_collectonView2页面,它显示_collectionView页面。

_collectionView是在界面构建器中创建的,我猜测每次viewWillAppear运行时都会重新创建它,只是替换_collectionView2。我试图在viewWillAppear中隐藏它,例如[_collectionView setHidden:YES];并隐藏它并显示_collectionView2。

单个文件显示:

enter image description here

以下是UISegmentedControl的代码:

- (void)displayTypeSegmentSelected
{
    _selectedDisplayTypeIndex = [_displayTypeControl selectedSegmentIndex];


    if (_selectedDisplayTypeIndex == 0) {
        NSLog(@"Single file item view selected");
        _fromCollectionView = _collectionView;
        _toCollectionView = _collectionView2;
    } else {
        NSLog(@"Grid style view selected");
        _fromCollectionView = _collectionView2;
        _toCollectionView = _collectionView;
    }

    [_fromCollectionView removeFromSuperview];

    [_toCollectionView setFrame:[_superView bounds]];
    [_superView addSubview:_toCollectionView];
    [self createFilterBar];

}

问题:

有没有办法让UINavigation实例知道是从单个文件显示视图导航到的,这样当我点击它时它不会重新加载_collectionView?但需要回到_collectionView?

更新 - 按要求提供viewWillAppear代码:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSLog(@"view will appear");



    // Create flow layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    // Set up margins, sizes etc
    [layout setHeaderReferenceSize:CGSizeMake(50,93)];
    [layout setItemSize:CGSizeMake(300, 500)];
    [layout setMinimumLineSpacing:0];
    [layout setMinimumInteritemSpacing:0];
    [layout setSectionInset:UIEdgeInsetsMake(1, 0, 40, 0)];
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];

    // Initialise collection view 2 with frame, attach layout
    _collectionView2 = [[UICollectionView alloc] initWithFrame:[[_thisController view] frame] collectionViewLayout:layout];

    // Set background colour, delegate and dataSource
    [_collectionView2 setBackgroundColor:[UIColor whiteColor]];
    [_collectionView2 setDelegate:_thisController];
    [_collectionView2 setDataSource:_thisController];

    // Grab nib header nib file and give it a reuse identifier
    [_collectionView2 registerNib:[UINib nibWithNibName:@"VAGHeaderLabelReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView2"];

    // Grab cell nib file and give a reuse identifier
    [_collectionView2 registerNib:[UINib nibWithNibName:@"VAGGarmentCell2" bundle:nil] forCellWithReuseIdentifier:@"Cell2"];

    // Add as subview of this controller
    [_collectionView2 setFrame:[[_thisController view] bounds]];
   // [[_thisController view] addSubview:_collectionView2];



    [_thisController setTitle:@"S  H  O  P"];

    // Set to yes so back button isn't hidden and disabled. Note that back title is set to blank on controller that pushes to this one
    [[_thisController navigationItem] setLeftItemsSupplementBackButton: YES];

    // Create nav bar buttons
    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"magnify_glass.png"] style:UIBarButtonItemStylePlain target:_thisController action:@selector(searchButtonTapped)];
    [[_thisController navigationItem] setLeftBarButtonItem:searchButton];

    UIBarButtonItem *shoppingCartButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shopping_cart.png"] style:UIBarButtonItemStylePlain target:_thisController action:@selector(shoppingCartButtonTapped)];

    UIBarButtonItem *addFavouriteButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_fav_heart.png"] style:UIBarButtonItemStylePlain target:_thisController action:@selector(addFavouriteButtonTapped)];

    [[_thisController navigationItem] setRightBarButtonItems:@[shoppingCartButton, addFavouriteButton] animated: YES];

    // Reset refine button colour back to clear after returning from refineButtonTableViewController
    [_refineButton setBackgroundColor:[UIColor clearColor]];

    [_navigationBar setBarTintColor:[UIColor whiteColor]];
    [_navigationBar setTranslucent:NO];

    // Create filter bar
    [self createFilterBar];

2 个答案:

答案 0 :(得分:0)

我在项目中遇到了类似的情况,我们决定只更改一个集合视图的单元格并重新加载集合视图,而不是使用两个单独的集合视图。我认为这样可以解决您的问题并让您不必担心实施,因为您继续只管理一个集合视图。您只需检查cellForItemAtIndexPath方法中的当前可视化选择。从概念上讲,您将其实现为:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentSelection == kSingleView) {
        return [self cellForSingleView];
    } else {
        return [self cellForGridView];
    }
}

如果您有多种可视化样式,则显然需要检查更多值。然后在viewWillAppear中,您只需重新加载集合视图。或者,您可能只想在viewDidLoad中加载集合视图,因为当视图控制器被弹出时不会被调用#34;至。它也是使用枚举来定义样式的好时机,因为枚举比常量更具可扩展性。

修改

为此,您还必须检查当前选择并在sizeForItemAtIndexPath中设置所需的单元格大小:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.currentSelection == kSingleView) {
        // return size of Single View Cell
    } else {
        // return size of Grid View Cell
    }
}

另外,请确保为每个单元格使用不同的单元格标识符。

答案 1 :(得分:0)

我通过以模态方式呈现视图并使用取消按钮解除它来解决此问题。 Subclassed新控制器并在该文件中创建了一个连接到我的取消按钮的动作,其代码在点击取消按钮时关闭控制器。

#import "VAGRefineResultsTableViewController.h"

@interface VAGRefineResultsTableViewController ()


@end

@implementation VAGRefineResultsTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do stuff here
}

- (IBAction)cancelButtonTapped:(id)sender {
    [self dismissViewControllerAnimated:YES completion:NULL];
}