您的位置:首页 > 移动开发 > IOS开发

iOS 点击tableView的cell,让其滚到屏幕顶部

2015-04-30 15:02 351 查看

点击tableView的cell,让其滚到屏幕顶部,很多电商的分类模块,都采用这种做法

1. 示例代码

- (void)viewDidLoad {

[super viewDidLoad];

[self addTableView];

}

#pragma mark - 创建tableView

- (void)addTableView

{

UITableView *tableView = [[UITableView alloc]init];

tableView.frame = self.view.bounds;

tableView.delegate = self;

tableView.dataSource = self;

[self.view addSubview:tableView];

}

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

{

// 1.创建cell

static NSString *ID = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

if (cell == nil) {

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

}

// 2.设置cell的数据

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

return cell;

}

#pragma mark - 点击cell调用

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

{

[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; // 关键代码

}

2. 参数说明:

UITableViewScrollPositionNone, 无所谓,置顶,置底都可以,只要最快出现在屏幕范围内

UITableViewScrollPositionTop, 点击的那行置顶

UITableViewScrollPositionMiddle, 点击的那行置为中间

UITableViewScrollPositionBottom 点击的那行置底
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: