技术好文:UITableView(可滚动到顶部和底部)

简介: 技术好文:UITableView(可滚动到顶部和底部)

#import "RootViewController.h"


#define width 【UIScreen mainScreen】.bounds.size.width


#define height 【UIScreen mainScreen】.bounds.size.height


#define topH 64


@interface RootViewController ()


@property (nonatomic, strong) UITableView tableView;


@property (nonatomic, strong) NSMutableArray rowData;


@end


@implementation RootViewController


- (void)loadView


{


【super loadView】;


// 初始化view


UIView aView = 【【UIView alloc】 initWithFrame:CGRectMake(0, 0, width, topH)】;


aView.backgroundColor = 【UIColor grayColor】;


【self.view addSubview:aView】;


// 初始化leftButton


SEL leftSel = NSSelectorFromString(@"scrollToTop:");


UIButton leftBtn = 【self setupButtonWithTitle:@"top" withFrame:CGRectMake(0, 20, 80, topH - 20) withSelector:leftSel】;


【self.view addSubview:leftBtn】;


// 初始化rigthButton


SEL rightSel = NSSelectorFromString(@"scrollToBottom:");


UIButton rightBtn = 【self setupButtonWithTitle:@"bottom" withFrame:CGRectMake(width - 80 - 20, 20, 80, topH-20) withSelector:rightSel】;


【self.view addSubview:rightBtn】;


// 初始化_tableView


self.tableView = 【【UITableView alloc】 initWithFrame:CGRectMake(0, topH,width, height - topH) style:UITableViewStylePlain】;


self.tableView.dataSource = self;


self.tableView.delegate = self;


【self.view addSubview:self.tableView】;


}


/**


初始化Button



@param aTitle Button的标题


@param aFrame Button的框架


@param aSelector Button的方法



@return Button


/


- (UIButton )setupButtonWithTitle:(NSString )aTitle withFrame:(CGRect)aFrame withSelector:(SEL)aSelector


{


UIButton btn = 【UIButton buttonWithType:UIButtonTypeRoundedRect】;


btn.frame = aFrame;


【btn setTitle:aTitle forState:0】;


btn.titleLabel.font = 【UIFont systemFontOfSize:25】;


【btn addTarget:self action:aSelector forControlEvents:UIControlEventTouchUpInside】;


return btn;


}


- (void)viewDidLoad {


【super viewDidLoad】;


【self loadData】;


}


/*


加载数据


/


-(void)loadData


{


if (self.rowData!=nil)


{


【self.rowData removeAllObjects】;


self.rowData=nil;


}


self.rowData = 【【NSMutableArray alloc】 init】;


for (int i=0 ; i[span style="color: rgba(128, 0, 128, 1)">100;i++)


{


【self.rowData addObject:【NSString stringWithFormat:@"Row: %i",i】】;


}


【self.tableView reloadData】;


}


- (void)scrollToTop:(UIButton )sender


{


NSIndexPath topRow = 【NSIndexPath //代码效果参考:http://www.lyjsj.net.cn/wx/art_23823.html

indexPathForRow:0 inSection:0】;

【self.tableView scrollToRowAtIndexPath:topRow atScrollPosition:UITableViewScrollPositionTop animated:YES】;


}


- (void)scrollToBottom:(UIButton )sender


{


NSIndexPath bottomRow = 【NSIndexPath indexPathForRow:【self.rowData count】-1 inSection:0】;


【self.tableView scrollToRowAtIndexPath:bottomRow atScrollPosition:UITableViewScrollPositionBottom animated:YES】;


}


#pragma mark -UITableView delegates-


- (NSInteger)numberOfSectionsInTableView:(UITableView )tableView {


return 1;


}


- (NSInteger)tableView:(UITableView )tableView numberOfRowsInSection:(NSInteger)section {


return 【self.rowData count】;


}


- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath )indexPath {


static NSString CellIdentifier = @"Cell";


UITableViewCell cell = (UITableViewCell *)【tableView dequeueReusableCellWithIdentifier:CellIdentifier】;


if (cell == nil) {


cell = 【【UITableViewCell alloc】 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier】;


}


cell.selectionStyle = UITableViewCellSelectionStyleNone;


cell.textLabel.text = 【self.rowData objectAtIndex:indexPath.row】;


return cell;


}


@end

相关文章
|
UED
禁止手机侧滑返回上一个页面的三种方法
禁止手机侧滑返回上一个页面的三种方法
1461 0
|
缓存 iOS开发 索引
Xcode清理缓存方法
Xcode清理缓存方法
522 0
|
iOS开发
iOS NSLineBreakMode 解释
iOS NSLineBreakMode 解释
478 0
|
iOS开发 开发者
iOS开发者后台添加新的UDID后,自动管理证书更新设备信息的方法
iOS开发者后台添加新的UDID后,自动管理证书更新设备信息的方法
726 0
iOS开发者后台添加新的UDID后,自动管理证书更新设备信息的方法
|
iOS开发
iOS 获取当前ViewController
iOS 获取当前ViewController
165 0
|
iOS开发
iOS 开发 - tableView内嵌scrollView时,在plus上滑动scrollView时和tableView有冲突
iOS 开发 - tableView内嵌scrollView时,在plus上滑动scrollView时和tableView有冲突
373 0
为View添加手势
为View添加手势
200 0
|
设计模式 程序员 iOS开发
iOS ScrollView的使用教程
iOS ScrollView的使用教程
457 0
iOS ScrollView的使用教程
GCD全解-dispatch_after/dispatch_time-t延迟操作
GCD全解-dispatch_after/dispatch_time-t延迟操作
617 0
|
数据安全/隐私保护
网络请求AFNetworking
网络请求AFNetworking
248 0

热门文章

最新文章