技术好文: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

相关文章
|
8月前
|
小程序 前端开发 容器
微信小程序隐藏右侧滚动条并可以滚动
微信小程序隐藏右侧滚动条并可以滚动
218 0
|
2月前
|
JavaScript 前端开发 容器
随着页面的滚动导航条跟着滚动
随着页面的滚动导航条跟着滚动
uniapp滚动条置顶效果、自定义页面滚动条的位置(整理)
uniapp滚动条置顶效果、自定义页面滚动条的位置(整理)
|
2月前
|
前端开发
Flutter笔记:光影动画按钮、滚动图标卡片组等
Flutter笔记:光影动画按钮、滚动图标卡片组等
48 0
|
11月前
|
C++ Python
【Axure教程】鼠标滚动上下翻页效果
【Axure教程】鼠标滚动上下翻页效果
网站顶部的跑马灯特效代码
跑马灯的方法很多,其中最简单的是采用一句Html代码来实现
134 0
网站顶部的跑马灯特效代码
|
移动开发
【笔记】一行代码完成——h5页面上滑图片渐隐
一行代码完成——h5页面上滑图片渐隐
137 0
刚进入页面时UIScrollView没有滑动到顶部
刚进入页面时UIScrollView没有滑动到顶部
60 0