最近做的項(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);