IOS UITableView Click the button to roll to the top

Source: Internet
Author: User

#import <UIKit/UIKit.h>@interface appdelegate:uiresponder <UIApplicationDelegate>  *window; @end
#import "AppDelegate.h"#import "RootViewController.h"@interfaceappdelegate ()@end@implementationappdelegate-(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]; returnYES;}@end
#import <UIKit/UIKit.h>@interface  rootviewcontroller:uiviewcontroller@end 
#import "RootViewController.h"@interfaceRootviewcontroller () <UITableViewDataSource,UITableViewDelegate>{UITableView*_tableview; Nsmutablearray*datas;}@end@implementationRootviewcontroller- (void) loadview{[Super Loadview]; _tableview= [[UITableView alloc] Initwithframe:cgrectmake (0, -, [UIScreen mainscreen].bounds.size.width, [UIScreen mainscreen].bounds.size.height- -) Style:uitableviewstyleplain]; _tableview.datasource=Self ; _tableview.Delegate=Self ;        [Self.view Addsubview:_tableview]; [Self Initializebuttonwithframe:cgrectmake (0,0, -, -) Title:@"Roll to top"Action: @selector (scrolltotop:)]; [Self Initializebuttonwithframe:cgrectmake ([UIScreen mainscreen].bounds.size.width- -,0, -, -) Title:@"Roll to the bottom ."Action: @selector (Scrolltobuttom:)];}- (void) viewdidload {[Super viewdidload]; Self.title=@"Test"; Datas=[[Nsmutablearray alloc] init];  for(inti =0; I < -; i++) {NSString*STR = [NSString stringWithFormat:@"row:%d", I];    [Datas ADDOBJECT:STR]; }    }/** * Initialize button * * @param frame size * @param title * method of @param asel button*/- (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 (@"Roll to top"); Nsindexpath*toprow = [Nsindexpath indexpathforrow:0Insection:0]; [_tableview scrolltorowatindexpath:toprow atscrollposition:uitableviewscrollpositiontop Animated:YES];}- (void) Scrolltobuttom: (uibutton*) sender{NSLog (@"Roll to the bottom ."); Nsindexpath*buttomrow = [Nsindexpath indexPathForRow:datas.count-1Insection:0]; [_tableview scrolltorowatindexpath:buttomrow atscrollposition:uitableviewscrollpositionbottom Animated:YES];}#pragmaMark-TableView's agent---(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return 1;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{returnDatas.count;}-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *identifier =@"Cell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:identifier]; if(!cell) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier]; } Cell.textLabel.text=Datas[indexpath.row]; returncell;}@end

IOS UITableView Click the button to roll to the top

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.