iOS UITableView點擊按鈕滾到頂部

來源:互聯網
上載者:User

標籤:

#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

 

iOS UITableView點擊按鈕滾到頂部

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.