• 133自动滚动到被选中单元格(扩展知识:滚动到头部和底部)


    效果如下:

    ViewController.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface ViewController : UITableViewController
    4 @property (strong, nonatomic) NSMutableArray *mArrDataSource;
    5 
    6 @end

    ViewController.m

      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 - (void)layoutUI;
      5 - (UIBarButtonItem *)barButtonItemForToolbar:(NSString *)title selectorName:(NSString *)selectorName;
      6 - (void)scrollToTopDidPush:(UIBarButtonItem *)sender;
      7 - (void)scrollToSelectedRowDidPush:(UIBarButtonItem *)sender;
      8 - (void)scrollToBottomDidPush:(UIBarButtonItem *)sender;
      9 @end
     10 
     11 @implementation ViewController
     12 #define kRowCount 64
     13 
     14 - (void)viewDidLoad {
     15     [super viewDidLoad];
     16     
     17     [self layoutUI];
     18 }
     19 
     20 - (void)didReceiveMemoryWarning {
     21     [super didReceiveMemoryWarning];
     22     // Dispose of any resources that can be recreated.
     23 }
     24 
     25 - (void)viewDidAppear:(BOOL)animated {
     26     [super viewDidAppear:animated];
     27     
     28     [self.navigationController setNavigationBarHidden:NO animated:animated];
     29     [self.navigationController setToolbarHidden:NO animated:animated];
     30 }
     31 
     32 - (void)layoutUI {
     33     _mArrDataSource = [[NSMutableArray alloc] initWithCapacity:kRowCount];
     34     for (NSUInteger i=0; i<kRowCount; i++) {
     35         [_mArrDataSource addObject:[NSNumber numberWithUnsignedInteger:i]];
     36     }
     37     [self.tableView setAllowsSelection:YES]; //设置在普通状态中,是否允许选中;默认值为YES
     38     [self.tableView setAllowsSelectionDuringEditing:YES]; //设置在编辑状态中,是否允许选中;默认值为YES
     39     
     40     self.navigationItem.title = @"自动滚动到被选中单元格";
     41     UIBarButtonItem *barBtnScrollToTop = [self barButtonItemForToolbar:@"滚动到头部" selectorName:@"scrollToTopDidPush:"];
     42     UIBarButtonItem *barBtnScrollToSelectedRow = [self barButtonItemForToolbar:@"滚动到被选中单元格" selectorName:@"scrollToSelectedRowDidPush:"];
     43     UIBarButtonItem *barBtnScrollToBottom = [self barButtonItemForToolbar:@"滚动到底部" selectorName:@"scrollToBottomDidPush:"];
     44     self.toolbarItems = @[barBtnScrollToTop, barBtnScrollToSelectedRow, barBtnScrollToBottom];
     45 }
     46 
     47 - (UIBarButtonItem *)barButtonItemForToolbar:(NSString *)title selectorName:(NSString *)selectorName {
     48     UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithTitle:title
     49                                                                    style:UIBarButtonItemStyleDone
     50                                                                   target:self
     51                                                                   action:NSSelectorFromString(selectorName)];
     52     return barBtnItem;
     53 }
     54 
     55 - (void)scrollToTopDidPush:(UIBarButtonItem *)sender {
     56     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
     57                           atScrollPosition:UITableViewScrollPositionTop
     58                                                       animated:YES];
     59 }
     60 
     61 - (void)scrollToSelectedRowDidPush:(UIBarButtonItem *)sender {
     62     [self.tableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionNone
     63                                                       animated:YES];
     64     
     65     /*
     66      typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
     67      UITableViewScrollPositionNone,
     68      UITableViewScrollPositionTop,
     69      UITableViewScrollPositionMiddle,
     70      UITableViewScrollPositionBottom
     71      };                // scroll so row of interest is completely visible at top/center/bottom of view
     72      */
     73 }
     74 
     75 - (void)scrollToBottomDidPush:(UIBarButtonItem *)sender {
     76     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(kRowCount - 1) inSection:0]
     77                           atScrollPosition:UITableViewScrollPositionBottom
     78                                   animated:YES];
     79 }
     80 
     81 #pragma mark - TableView
     82 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
     83     return @"列表";
     84 }
     85 
     86 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     87     return 1;
     88 }
     89 
     90 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     91     return kRowCount;
     92 }
     93 
     94 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     95     static NSString *cellIdentifier = @"cellIdentifier";
     96     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     97     if (!cell) {
     98         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     99     }
    100     
    101     NSInteger row = indexPath.row;
    102     cell.textLabel.text = [_mArrDataSource[row] stringValue];
    103     
    104     return cell;
    105 }
    106 
    107 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    108     
    109 }
    110 
    111 @end

    AppDelegate.h

    1 #import <UIKit/UIKit.h>
    2 
    3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
    4 @property (strong, nonatomic) UIWindow *window;
    5 @property (strong, nonatomic) UINavigationController *navigationController;
    6 
    7 @end

    AppDelegate.m

     1 #import "AppDelegate.h"
     2 #import "ViewController.h"
     3 
     4 @interface AppDelegate ()
     5 @end
     6 
     7 @implementation AppDelegate
     8 
     9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    11     ViewController *viewController = [[ViewController alloc] init];
    12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    13     _window.rootViewController = _navigationController;
    14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
    15     [_window makeKeyAndVisible];
    16     return YES;
    17 }
    18 
    19 - (void)applicationWillResignActive:(UIApplication *)application {
    20 }
    21 
    22 - (void)applicationDidEnterBackground:(UIApplication *)application {
    23 }
    24 
    25 - (void)applicationWillEnterForeground:(UIApplication *)application {
    26 }
    27 
    28 - (void)applicationDidBecomeActive:(UIApplication *)application {
    29 }
    30 
    31 - (void)applicationWillTerminate:(UIApplication *)application {
    32 }
    33 
    34 @end
  • 相关阅读:
    ThinkPHP 小于5.0.24 远程代码执行高危漏洞 修复方案
    Nginx负载均衡配置与负载策略
    【高级】PHP-FPM和Nginx的通信机制
    快手、抖音、微视类短视频SDK接入教程,7步就能搞定
    我是怎么一步步用go找出压测性能瓶颈
    一个域名引发的血案……
    开发效率太低?您可能没看这篇文章
    想熟悉PostgreSQL?这篇就够了
    tee命令
    linux下常用的日志分析命令
  • 原文地址:https://www.cnblogs.com/huangjianwu/p/4581237.html
Copyright © 2020-2023  润新知