iOS 商城类 app 首页的实现
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
iOS 商城類 app 首頁的實(shí)現(xiàn)
????很多人做 iOS開發(fā)的人員都應(yīng)該寫過這樣的界面,但是呢,具體怎么去優(yōu)化寫這樣的界面是我們需要考慮的,對(duì)于這樣的界面動(dòng)輒上千行代碼在控制器里面"活躍"著,后期維護(hù)成本大大增加,現(xiàn)在我簡(jiǎn)單的說一下我的實(shí)現(xiàn)思路,這樣的界面無非就是 collectionView 或者 tableView. 舉個(gè)簡(jiǎn)單的例子,以 tableView 為例吧,絕大數(shù) app 首頁基本都是第一個(gè)分區(qū)是個(gè) bannar, 其實(shí)是分類,其次可能還是分類,最后是列表,一般這個(gè)時(shí)候,我的習(xí)慣是將每一塊自定義 view, 然后當(dāng)做 tableView 的 sectionHeaderView, 最后一個(gè)是 cell,當(dāng)然你也可以將每一塊攜程 cell 也是可以的,因?yàn)?sectionHeaderView 也是可以復(fù)用的,當(dāng)然,如果就是三四個(gè)分區(qū)是否復(fù)用其實(shí)差不多(如果大家覺得有必要復(fù)用,請(qǐng)記得在下面留言,然后我們一起探討一下嘛). 上個(gè)圖,很丑,簡(jiǎn)單的看一下就好了,不要介意
1.第一部分代碼:?
#import "SectionHeaderViewOne.h"@interface SectionHeaderViewOne ()@property (nonatomic,strong) UIImageView *hotel_image; @property (nonatomic,strong) UILabel *hotel_type; @property (nonatomic,strong) UIButton *hotel_rate; @property (nonatomic,strong) UIButton *hotel_address;@end@implementation SectionHeaderViewOne- (instancetype)initWithFrame:(CGRect)frame {self = [super initWithFrame:frame];if (self) {[self setupUI];}return self; }- (void)setupUI {[self addSubview:self.hotel_image];[self addSubview:self.hotel_type];[self addSubview:self.hotel_rate];[self addSubview:self.hotel_address]; }#pragma mark --- 懶加載 ---- - (UIImageView *)hotel_image {if (!_hotel_image) {_hotel_image = [[UIImageView alloc] init];[_hotel_image setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 50)];_hotel_image.image = [UIImage imageNamed:@"timg.jpg"];_hotel_image.contentMode = UIViewContentModeScaleToFill;}return _hotel_image; }-(UILabel *)hotel_type {if (!_hotel_type) {_hotel_type = [[UILabel alloc] init];[_hotel_type setFrame:CGRectMake(10, self.frame.size.height - 100, 100, 50)];_hotel_type.font = [UIFont systemFontOfSize:18];_hotel_type.text = @"7天";_hotel_type.textColor = [UIColor whiteColor];}return _hotel_type; }- (UIButton *)hotel_rate {if (!_hotel_rate) {_hotel_rate = [UIButton buttonWithType:UIButtonTypeCustom];[_hotel_rate setFrame:CGRectMake(self.frame.size.width - 100, self.hotel_type.frame.origin.y, 100, 50)];[_hotel_rate setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];_hotel_rate.titleLabel.font = [UIFont systemFontOfSize:18];[_hotel_rate setTitle:@"查看評(píng)價(jià)" forState:UIControlStateNormal];}return _hotel_rate; }- (UIButton *)hotel_address {if (!_hotel_address) {_hotel_address = [UIButton buttonWithType:UIButtonTypeCustom];[_hotel_address setFrame:CGRectMake(10, CGRectGetMaxY(self.hotel_image.frame), self.frame.size.width - 20, 50)];[_hotel_address setTitle:@"地址: 北京天安門" forState:UIControlStateNormal];[_hotel_address setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];[_hotel_address setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];_hotel_address.titleLabel.font = [UIFont systemFontOfSize:17];}return _hotel_address; }2.第二部分的話,就稍微麻煩點(diǎn),因?yàn)樯厦媲短琢艘粋€(gè)?collectionView
先自定義 collectionView, 在自定義 collectionViewCell, 最后也可以自定義一個(gè) view, 將其作為 collectionView 的父視圖 (我是這樣寫的,我個(gè)人覺得沒有多大的必要, 簡(jiǎn)單事物復(fù)雜化了)
collectiomView.m 的代碼?
#import "CollectionView.h" #import "CollectionViewCell.h"@interface CollectionView () <UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>@endstatic NSString *kIdentify = @"CollectionViewCell";@implementation CollectionView-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {if (self = [super initWithFrame:frame collectionViewLayout:layout]) {self.delegate = self;self.dataSource = self;self.alwaysBounceVertical = YES;[self registerNib:[UINib nibWithNibName:kIdentify bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kIdentify];}return self; }#pragma mark --- 代理 ---- #pragma mark 設(shè)置CollectionView的組數(shù) - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{return 1; }#pragma mark 設(shè)置CollectionView每組所包含的個(gè)數(shù) - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return self.imgArrays.count; }#pragma mark 設(shè)置CollectionCell的內(nèi)容 // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kIdentify forIndexPath:indexPath];cell.type_img.image = [UIImage imageNamed:(self.imgArrays[indexPath.row])];return cell; }#pragma mark 定義每個(gè)UICollectionView的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{return CGSizeMake([UIScreen mainScreen].bounds.size.width / 5, 100); }#pragma mark 定義整個(gè)CollectionViewCell與整個(gè)View的間距 - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {return UIEdgeInsetsMake(0, 0, 0, 0);//(上、左、下、右) }#pragma mark 定義每個(gè)UICollectionView的橫向間距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {return 0; }#pragma mark 定義每個(gè)UICollectionView的縱向間距 - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {return 0; }#pragma mark 點(diǎn)擊CollectionView觸發(fā)事件 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{}#pragma mark 設(shè)置CollectionViewCell是否可以被點(diǎn)擊 - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{return YES; }collectionViewCell 用 xib 就好了,這個(gè)就不再啰嗦了
3. 自定義 tableView
#import "HotelTableView.h" #import "SectionHeaderViewOne.h" #import "SectionHeaderViewTwo.h" #import "CollectionView.h"@interface HotelTableView () <UITableViewDelegate,UITableViewDataSource>@end@implementation HotelTableView- (instancetype)init {self = [super init];if (self) {self.dataSource = self;self.delegate = self;UIView *footerView = [[UIView alloc] init];footerView.backgroundColor = [UIColor lightGrayColor];self.tableFooterView = footerView;}return self; }- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {if (section == 0) {return 0;}else if (section == 1){return 0;}else {return 10;} }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 3; }- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {static NSString *cell_id = @"cell_id";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];}cell.textLabel.text = @"簡(jiǎn)單的測(cè)試";return cell; }- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {if (section == 0) {SectionHeaderViewOne *headerView = [[SectionHeaderViewOne alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];return headerView;}else if (section == 1){SectionHeaderViewTwo *headerView = [[SectionHeaderViewTwo alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];headerView.imgArrays = @[@"04",@"10",@"42",@"51",@"70",@"80-2",@"80",@"10",@"04",@"51"];return headerView;}else {UILabel *label = [[UILabel alloc] init];[label setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];label.text = @"入住日期,3月24日";label.font = [UIFont systemFontOfSize:18];return label;} }- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 100; }- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {if (section == 2) {return 60;}else {return 200;} }- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {return 10; }4. 將 tableView 添加到控制器上面就好了
#import "ViewController.h" #import "HotelTableView.h"@interface ViewController ()@property (nonatomic,strong) HotelTableView *tableView;@end@implementation ViewController-(HotelTableView *)tableView {if (!_tableView) {_tableView = [[HotelTableView alloc] init];[_tableView setFrame:self.view.bounds];}return _tableView; }- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.[self.view addSubview:self.tableView]; }- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated. }?
以上就可以簡(jiǎn)單實(shí)現(xiàn)上面圖的界面了,這樣控制器里面看起來是不是很簡(jiǎn)潔,因?yàn)檫壿嬏幚砜梢韵鄳?yīng)的分散到 tableView 以及子視圖當(dāng)中, 哪怕界面邏輯修改,維護(hù)起來成本也不至于太高,這是我的想法,歡迎各路大神拍磚
?
轉(zhuǎn)載于:https://my.oschina.net/alanTang123/blog/867363
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的iOS 商城类 app 首页的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 索引器方法
- 下一篇: LINUX服务器搭建和常用配置介绍