UITableView基礎(chǔ)設(shè)置

1.UITableView懶加創(chuàng)建

- (UITableView *)ta{

if (_ta == nil) {

//_ta = [UITableView new];默認(rèn)方式創(chuàng)建plain

//Group風(fēng)格會(huì)有頭和腳,而設(shè)置make.top是從頭開始到self.top

_ta = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];//帶風(fēng)格地創(chuàng)建

_ta.delegate = self;

_ta.dataSource = self;

[self.view? addSubview:_ta];

[_ta mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.bottom.right.equalTo(0);

make.top.equalTo(20);

}];

//去除多余部分cell

_tableView.tableFooterView = [UIView new];

}

return _ta;

}

2.UITableView的代理類方法

1)設(shè)置cell

//創(chuàng)建幾個(gè)組,默認(rèn)值為1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;

}

//每個(gè)組有幾行

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

return 10;

}

//cell的創(chuàng)建

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

static NSString *identifier = @"iphone";

//使用回內(nèi)存重用的方式創(chuàng)建cell

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

#warning cell是有風(fēng)格的,在創(chuàng)建的時(shí)候

//帶有風(fēng)格(共4種)地創(chuàng)建cell,并標(biāo)記重用標(biāo)志

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

//附加視圖

cell.accessoryType = 1;

//被點(diǎn)擊后的選中/高亮顏色

cell.selectionStyle = 1;

//自定義選中視圖

UIView *bgView = [UIView new];

bgView.backgroundColor = [UIColor greenColor];

cell.selectedBackgroundView = bgView;

//自定義輔助視圖

UISwitch *swi = [UISwitch new];

cell.accessoryView = swi;

//自定義背景視圖

UIView *bV = [UIView new];

bV.backgroundColor = [UIColor lightGrayColor];

cell.backgroundView = bV;

//自定義頭像視圖

cell.imageView.image = [UIImage imageNamed:@"anno"];

}

NSString *sectionTitle = [self.arr objectAtIndex:indexPath.section];

cell.textLabel.text = [NSString stringWithFormat:@"%@%ld",sectionTitle, indexPath.row];

cell.detailTextLabel.text = @"詳細(xì)信息";

return cell;

}

2)設(shè)置頭部和尾部title

//每個(gè)分區(qū)頭部顯示的題目

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return @"頭部";

}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

return @"尾部";

}

3)設(shè)置cell,頭部,尾部高度

//自定義行高默認(rèn)是44像素

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 50;

}

//自定義組頭高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 40;

}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

return 40;

}

4)添加索引

//添加右側(cè)分區(qū)索引值- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{

return self.arr;

}

//特殊指定點(diǎn)擊分區(qū)索引值之后的跳轉(zhuǎn)

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{

return index;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時(shí)使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,297評(píng)論 3 38
  • 版權(quán)聲明:未經(jīng)本人允許,禁止轉(zhuǎn)載. 1. TableView初始化 1.UITableView有兩種風(fēng)格:UITa...
    蕭雪痕閱讀 2,990評(píng)論 2 10
  • 序引 本系列文章將介紹iOS開發(fā)中的UITableView控件,將會(huì)分成四篇文章完整的講述UITableView的...
    yetCode閱讀 2,409評(píng)論 3 40
  • 雨從昨天晚上一直下到現(xiàn)在,這一天照樣是充實(shí)而又忙碌。 今天收到的喜訊是同學(xué)們時(shí)隔七年終于要團(tuán)聚了。導(dǎo)師親自號(hào)召離得...
    華麗的美麗麗閱讀 166評(píng)論 0 0

友情鏈接更多精彩內(nèi)容