zoukankan      html  css  js  c++  java
  • iOS 点击tableView的cell,让其滚到屏幕顶部

    点击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  点击的那行置底

  • 相关阅读:
    正则表达式  语法
    正则表达式  语法
    SQL Server 删除日志文件
    SQL Server 删除日志文件
    C# CLR简介
    C# CLR简介
    理解 C# 项目 csproj 文件格式的本质和编译流程
    理解 C# 项目 csproj 文件格式的本质和编译流程
    De4Dot+Reflector 支持多种反混淆
    De4Dot+Reflector 支持多种反混淆
  • 原文地址:https://www.cnblogs.com/oumygade/p/4468943.html
Copyright © 2011-2022 走看看