UITableView

最近做的項(xiàng)目中用的UITableView比較多,在這里對(duì)UITableView的使用簡(jiǎn)單做一下總結(jié)荣堰。

@protocol UITableViewDelegate常用的幾個(gè)方法:

//高度

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

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

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

//預(yù)估高度

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);

//HeadView,FootView

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;? // custom view for header. will be adjusted to default or specified header height

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;? // custom view for footer. will be adjusted to default or specified footer height

@protocol UITableViewDataSource 常用的幾個(gè)方法:

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

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

//Section

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;? ? ? ? ? ? ? // Default is 1 if not implemented

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;? ? // fixed font style. use custom view (UILabel) if you want something different

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

1、不給預(yù)估高度流程

1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView拧咳;

2)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section干厚;

3)- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section礁蔗;

4)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section壤短;

5)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath设拟;

6)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

2久脯、給預(yù)估高度流程

ios8:

1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView纳胧;

2)- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;

3)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

4)- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section帘撰;

5)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section躲雅;

6)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

7)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath骡和;

ios7:

1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

2)- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;

3)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section相寇;

4)- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section慰于;

5)- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

6)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath唤衫;

7)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath婆赠;

3、對(duì)section和row進(jìn)行添加和刪除

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)insertRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

在調(diào)用該系列方法時(shí)佳励,需要加上一下片段:

[_tableView beginUpdates];

休里。。赃承。妙黍。。瞧剖。方法拭嫁。可免。。做粤。浇借。。

[_tableView? endUpdates];

4怕品、對(duì)某個(gè)section和row進(jìn)行重新加載

- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)reloadRowsAtIndexPaths:(NSArray*)indexPaths withRowAnimation:(UITableViewRowAnimation)animation妇垢;

5、使UItableView自帶刪除功能

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

? ? ? ?return UITableViewCellEditingStyleDelete肉康;

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

? ? ? ? [tableView beginUpdates];

? ? ? ? [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];

? ? ? ? [tableView? endUpdates];

}

6闯估、自定義cell

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

{

CommonProblemTableViewCell* cell = [CommonProblemTableViewCell cellWithTableView:tableView];

return cell;

}

@implementation CommonProblemTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView

{

static NSString *identifier = @"CommonProblemTableViewCell";

CommonProblemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[CommonProblemTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

}

return cell;

}

這么做將cell的復(fù)用代碼都搬到cell里面去了,起到很好的封裝作用迎罗,使UIViewController不至于那么臃腫睬愤;

7、點(diǎn)擊cell不顯示系統(tǒng)陰影

tableView.separatorStyle = UITableViewCellAccessoryNone;

8纹安、自定義選中cell的View

UIView *viewBg = [[UIView alloc] initWithFrame:cell.frame];

viewBg.backgroundColor = BACKGROUND_COLOR;

cell.selectedBackgroundView = viewBg;

9尤辱、點(diǎn)擊cell不停留在cell上

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[tableView deselectRowAtIndexPath:indexPath animated:NO];

}

10、實(shí)時(shí)計(jì)算cell的高度

[self layoutIfNeeded];

NSLog(@"the detailLabel rect minY is %f",CGRectGetMinY(self.detailLabel.frame));

NSLog(@"the detailLabel rect maxY is %f",CGRectGetMaxY(self.detailLabel.frame));

self.bg.frame = CGRectMake(0, 0, MAIN_SCREEN_WIDTH, CGRectGetMaxY(self.detailLabel.frame));

self.frame = self.bg.frame;

self.activityRewardModel.cellHeight = CGRectGetMaxY(self.detailLabel.frame);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末厢岂,一起剝皮案震驚了整個(gè)濱河市光督,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌塔粒,老刑警劉巖结借,帶你破解...
    沈念sama閱讀 222,252評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異卒茬,居然都是意外死亡船老,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門圃酵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來柳畔,“玉大人,你說我怎么就攤上這事郭赐⌒胶” “怎么了?”我有些...
    開封第一講書人閱讀 168,814評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵捌锭,是天一觀的道長俘陷。 經(jīng)常有香客問我,道長观谦,這世上最難降的妖魔是什么拉盾? 我笑而不...
    開封第一講書人閱讀 59,869評(píng)論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮坎匿,結(jié)果婚禮上盾剩,老公的妹妹穿的比我還像新娘雷激。我一直安慰自己,他們只是感情好告私,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評(píng)論 6 398
  • 文/花漫 我一把揭開白布屎暇。 她就那樣靜靜地躺著,像睡著了一般驻粟。 火紅的嫁衣襯著肌膚如雪根悼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評(píng)論 1 312
  • 那天蜀撑,我揣著相機(jī)與錄音挤巡,去河邊找鬼。 笑死酷麦,一個(gè)胖子當(dāng)著我的面吹牛矿卑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播沃饶,決...
    沈念sama閱讀 41,010評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼母廷,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了糊肤?” 一聲冷哼從身側(cè)響起琴昆,我...
    開封第一講書人閱讀 39,924評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎馆揉,沒想到半個(gè)月后业舍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡升酣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評(píng)論 3 342
  • 正文 我和宋清朗相戀三年舷暮,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片噩茄。...
    茶點(diǎn)故事閱讀 40,680評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡脚牍,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出巢墅,到底是詐尸還是另有隱情,我是刑警寧澤券膀,帶...
    沈念sama閱讀 36,362評(píng)論 5 351
  • 正文 年R本政府宣布君纫,位于F島的核電站,受9級(jí)特大地震影響芹彬,放射性物質(zhì)發(fā)生泄漏蓄髓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評(píng)論 3 335
  • 文/蒙蒙 一舒帮、第九天 我趴在偏房一處隱蔽的房頂上張望会喝。 院中可真熱鬧陡叠,春花似錦、人聲如沸肢执。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽预茄。三九已至兴溜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間耻陕,已是汗流浹背拙徽。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留诗宣,地道東北人膘怕。 一個(gè)月前我還...
    沈念sama閱讀 49,099評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像召庞,于是被迫代替她去往敵國和親岛心。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評(píng)論 2 361

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