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

  • 相关阅读:
    洛谷P3886 [JLOI2009]神秘的生物(插头dp)
    Leetcode 842 将数组拆分成斐波那契序列
    Leetcode 08.07 无重复字符串的排列组合
    Leetcode131 分割回文串
    Leetcode 516 最长回文子序列
    Leetcode08.12 N皇后
    Leetcode 813 最大平均值和分组
    Leetcode 79 单词搜索 二维平面上的回溯
    题解 洛谷 P4694 【[PA2013]Raper】
    跳表的基本认识
  • 原文地址:https://www.cnblogs.com/oumygade/p/4468943.html
Copyright © 2011-2022 走看看