UITableView

最近做的項目中用的UITableView比較多,在這里對UITableView的使用簡單做一下總結(jié)。

@protocol UITableViewDelegate常用的幾個方法:

//高度

- (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 常用的幾個方法:

- (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、對section和row進行添加和刪除

- (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)用該系列方法時,需要加上一下片段:

[_tableView beginUpdates];

。。。。。。方法。。。。。。

[_tableView? endUpdates];

4、對某個section和row進行重新加載

- (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、點擊cell不顯示系統(tǒng)陰影

tableView.separatorStyle = UITableViewCellAccessoryNone;

8、自定義選中cell的View

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

viewBg.backgroundColor = BACKGROUND_COLOR;

cell.selectedBackgroundView = viewBg;

9、點擊cell不停留在cell上

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

[tableView deselectRowAtIndexPath:indexPath animated:NO];

}

10、實時計算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)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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