UITableView和UICollectionView 頭尾視圖的創(chuàng)建

UITableView

頭(尾)視圖的View

TableHeaderView.h
@interface TableHeaderView : UITableViewHeaderFooterView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView;
@end

TableHeaderView.m
@interface TableHeaderView ()

@end
@implementation TableHeaderView
+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
    static NSString *headerID = @"header";
    TableHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headerID];
    if (headerView == nil) {
        headerView = [[TableHeaderView alloc]initWithReuseIdentifier:headerID];
    }
    return headerView;
}

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
         //添加頭(尾)視圖中的控件
   }
    return self;
}
@end

控制器

ViewController.m
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TableHeaderView *headerView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height)style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

......

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section     //頭視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section     //尾視圖
{
    _headerView = [TableHeaderView headerViewWithTableView:tableView];
    return _headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section   //頭視圖
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section    //尾視圖
{
    return  ;  //頭(尾)視圖的高度
}

@end

UICollectionView

頭(尾)視圖的View

CollectionHeaderView.h
@interface CollectionHeaderView : UICollectionReusableView
@end

CollectionHeaderView.m
@implementation CollectionHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //添加頭(尾)視圖中的控件      
    }
    return self;
}
@end

同理創(chuàng)建尾視圖的View: CollectionFooterView

控制器

ViewController.m
@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) CollectionHeaderView *headerView;
@property (nonatomic, strong) CollectionFooterView *footerView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height) collectionViewLayout:[UICollectionViewFlowLayout new]];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[CollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];             //注冊(cè)頭視圖
    [_collectionView registerClass:[CollectionFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];              //注冊(cè)尾視圖
}

......

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView = nil;
    if (kind == UICollectionElementKindSectionHeader) {       //頭視圖
        _headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        reusableView = _headerView;
    }else if (kind == UICollectionElementKindSectionFooter) {    //尾視圖
        _footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        _footerView.delegate = self;
        reusableView = _footerView;
    }
    return reusableView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(頭視圖的寬, 頭視圖的高);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(尾視圖的寬, 尾視圖的高);
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市把敞,隨后出現(xiàn)的幾起案子窗声,更是在濱河造成了極大的恐慌,老刑警劉巖钝满,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異申窘,居然都是意外死亡弯蚜,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)剃法,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)碎捺,“玉大人,你說(shuō)我怎么就攤上這事贷洲∈粘” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵优构,是天一觀的道長(zhǎng)诵叁。 經(jīng)常有香客問(wèn)我,道長(zhǎng)钦椭,這世上最難降的妖魔是什么拧额? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮彪腔,結(jié)果婚禮上侥锦,老公的妹妹穿的比我還像新娘。我一直安慰自己德挣,他們只是感情好恭垦,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著,像睡著了一般署照。 火紅的嫁衣襯著肌膚如雪祸泪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,604評(píng)論 1 305
  • 那天建芙,我揣著相機(jī)與錄音没隘,去河邊找鬼。 笑死禁荸,一個(gè)胖子當(dāng)著我的面吹牛右蒲,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播赶熟,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼瑰妄,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了映砖?” 一聲冷哼從身側(cè)響起间坐,我...
    開(kāi)封第一講書(shū)人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎邑退,沒(méi)想到半個(gè)月后竹宋,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡地技,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年蜈七,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片莫矗。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡飒硅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出作谚,到底是詐尸還是另有隱情三娩,我是刑警寧澤,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布食磕,位于F島的核電站尽棕,受9級(jí)特大地震影響喳挑,放射性物質(zhì)發(fā)生泄漏彬伦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一伊诵、第九天 我趴在偏房一處隱蔽的房頂上張望单绑。 院中可真熱鬧,春花似錦曹宴、人聲如沸搂橙。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)区转。三九已至苔巨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間废离,已是汗流浹背侄泽。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蜻韭,地道東北人悼尾。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像肖方,于是被迫代替她去往敵國(guó)和親闺魏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • 翻譯自“Collection View Programming Guide for iOS” 0 關(guān)于iOS集合視...
    lakerszhy閱讀 3,866評(píng)論 1 22
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)俯画、插件析桥、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,105評(píng)論 4 62
  • 夏天已經(jīng)來(lái)了,你是否準(zhǔn)備好“娜扎式"囤防曬拉宗? 但是單單防曬噴霧可不夠峦树! 但是防曬也是必須的 只不過(guò)娜扎太夸張了! ...
    星享加閱讀 259評(píng)論 0 0
  • 《西廂記》全名《崔鶯鶯待月西廂記》旦事,又稱“北西廂”魁巩,元代中國(guó)戲曲劇本,王實(shí)甫撰姐浮」人欤《西廂記》大約寫(xiě)于元貞、大德年間卖鲤,...
    妖妖z閱讀 1,002評(píng)論 22 23