美团HD(3)-加载分类导航数据

本文介绍了一个iOS应用中实现导航栏的设计方法,包括导航栏左侧Logo、类别、地区及排序等自定义项,并展示了如何通过Popover展示类别选择界面的具体实现。

 

DJHomeViewController.m

/** 设置导航栏左侧内容 */
- (void)setupLeftNavItem {

    // Logo
    UIImageView *logoView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_meituan_logo"]];
    UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithCustomView:logoView];
    
    // 类别
    DJNavItem *categoryView = [DJNavItem item];
    [categoryView addTarget:self action:@selector(onCategoryNavItemDidClick)];
    UIBarButtonItem *categoryItem = [[UIBarButtonItem alloc] initWithCustomView:categoryView];
    self.categoryItem = categoryItem;
    
    // 地区
    DJNavItem *areaView = [DJNavItem item];
    [areaView addTarget:self action:@selector(onAreaNavItemDidClick)];
    UIBarButtonItem *areaItem = [[UIBarButtonItem alloc] initWithCustomView:areaView];
    self.areaItem = areaItem;
    
    // 排序
    DJNavItem *sortView = [DJNavItem item];
    [sortView addTarget:self action:@selector(onSortNavItemDidClick)];
    UIBarButtonItem *sortItem = [[UIBarButtonItem alloc] initWithCustomView:sortView];
    self.sortItem = sortItem;
    
    self.navigationItem.leftBarButtonItems = @[logoItem,categoryItem,areaItem,sortItem];
    
}



#pragma mark - 导航按钮被点击
- (void)onCategoryNavItemDidClick {

    // 创建将要显示的Controller
    DJCategoryViewController *categoryVC = [[DJCategoryViewController alloc] init];
    // 设置控制器View的显示大小
    categoryVC.preferredContentSize = CGSizeMake(300, 360);
    // 设置Modal类型
    categoryVC.modalPresentationStyle = UIModalPresentationPopover;
    // 获取Popover
    UIPopoverPresentationController *categoryPopover = categoryVC.popoverPresentationController;
    // 相对于哪个View来显示
    categoryPopover.sourceView = self.categoryItem.customView;
    // 显示位置
    categoryPopover.sourceRect = self.categoryItem.customView.bounds;
    // 设置popover箭头的显示方向
    categoryPopover.permittedArrowDirections = UIPopoverArrowDirectionAny;
    // 跳转
    [self presentViewController:categoryVC animated:YES completion:nil];
    
}

DJNavDropView.m

#import "DJNavDropView.h"
#import "DJCategory.h"


@interface DJNavDropView()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;


@end



@implementation DJNavDropView

+ (instancetype)dropView {

   return[[[NSBundle mainBundle] loadNibNamed:@"DJNavDropView" owner:nil options:nil] lastObject];

}

- (void)setCategoryList:(NSArray *)categoryList {

    _categoryList = categoryList;
    
    // 刷新数据
    [self.mainTableView reloadData];

}


#pragma mark - TableView 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.categoryList.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ID = @"category";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    DJCategory *categoryItem = self.categoryList[indexPath.row];
    
    cell.textLabel.text = categoryItem.name;
    cell.imageView.image = [UIImage imageNamed:categoryItem.icon];
    
    return cell;
}

@end

最终效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值