zoukankan      html  css  js  c++  java
  • iOS UITableView点击按钮滚到顶部

    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    
    @end
    #import "AppDelegate.h"
    #import "RootViewController.h"
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate
    
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        
        
        self.window.rootViewController = [[RootViewController alloc] init];
        
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    @end
    #import <UIKit/UIKit.h>
    
    @interface RootViewController : UIViewController
    
    @end
    #import "RootViewController.h"
    
    @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
    {
        UITableView *_tableView;
        NSMutableArray *datas;
    }
    
    @end
    
    @implementation RootViewController
    
    - (void)loadView{
        [super loadView];
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self.view addSubview:_tableView];
        
        [self initializeButtonWithFrame:CGRectMake(0, 0,100, 64) title:@"滚到顶部" action:@selector(scrollToTop:)];
        [self initializeButtonWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 0, 100, 60) title:@"滚到底部" action:@selector(scrollToButtom:)];
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"测试";
        
        datas = [[NSMutableArray alloc] init];
        for (int i = 0; i < 30; i++) {
            NSString *str = [NSString stringWithFormat:@"row:%d",i];
            [datas addObject:str];
        }
        
    }
    /**
     *  初始化按钮
     *
     *  @param frame 尺寸
     *  @param title 标题
     *  @param aSEL  按钮的方法
     */
    - (void)initializeButtonWithFrame:(CGRect)frame title:(NSString*)title action:(SEL)aSEL{
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor grayColor];
        btn.frame = frame;
        [btn setTitle:title forState:0];
        [btn addTarget:self action:aSEL forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor grayColor];
        [self.view addSubview:btn];
    }
    
    - (void)scrollToTop:(UIButton*)sender{
        NSLog(@"滚到顶部");
        NSIndexPath *topRow = [NSIndexPath indexPathForRow:0 inSection:0];
        [_tableView scrollToRowAtIndexPath:topRow atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }
    
    - (void)scrollToButtom:(UIButton*)sender{
        NSLog(@"滚到底部");
        NSIndexPath *buttomRow = [NSIndexPath indexPathForRow:datas.count - 1 inSection:0];
        [_tableView scrollToRowAtIndexPath:buttomRow atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
    
    #pragma mark -- tableView的代理 --
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return datas.count;
    }
    
    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.textLabel.text = datas[indexPath.row];
        return cell;
    }
    
    @end
  • 相关阅读:
    [微信产品经理推荐] 有车一族福音,这个小程序能够帮到你很多忙,功能很逆天!
    微信小程序开闸,关于入口、推广、场景的一些观察与思考
    微信小程序体验(2):驴妈妈景区门票即买即游
    微信小程序的机会在于重新理解群组与二维码
    如何为你的微信小程序体积瘦身?
    体验报告:微信小程序在安卓机和苹果机上的区别
    微信小程序体验(1):携程酒店机票火车票
    张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题
    重点必看:小程序的服务范围限制有哪些?
    一些JS常用的方法
  • 原文地址:https://www.cnblogs.com/lantu1989/p/5205819.html
Copyright © 2011-2022 走看看