#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