美文网首页
UITableView

UITableView

作者: Kylin125 | 来源:发表于2016-03-24 21:28 被阅读0次

最近做的项目中用的UITableView比较多,在这里对UITableView的使用简单做一下总结。

@protocol UITableViewDelegate常用的几个方法:

//高度

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

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

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

//预估高度

- (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、不给预估高度流程

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、给预估高度流程

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;

在调用该系列方法时,需要加上一下片段:

[_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的复用代码都搬到cell里面去了,起到很好的封装作用,使UIViewController不至于那么臃肿;

7、点击cell不显示系统阴影

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);

相关文章

网友评论

      本文标题:UITableView

      本文链接:https://www.haomeiwen.com/subject/dlvrlttx.html