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

相关文章
|
算法 网络协议 数据安全/隐私保护
NTP网络时间同步协议详解
NTP协议是互联网上用于同步计算机时间的标准,它利用分布式时间服务器网络,通过分层管理和本地路由算法确保时间的准确性。协议涉及时间偏差、延迟和差量三个概念,用于测量与参考时钟的同步状态。基本NTP数据包包含48字节,如头部和时间戳,关键字段如跳跃指示器(LI)、版本号(VN)、模式(Mode)、层次(Stratum)等影响同步过程。协议还包括时间戳以记录不同阶段的时间点。NTP认证可选,用于数据安全。
NTP网络时间同步协议详解
|
前端开发
无法解锁/var/lib/dpkg/lock-frontend和无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend)【解决方案】
无法解锁/var/lib/dpkg/lock-frontend和无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend)【解决方案】
|
JavaScript 前端开发 Java
JavaScript基础知识点
JavaScript基础知识点
|
SQL 存储 关系型数据库
MySQL数据库案例实战教程:数据类型、语法与高级查询详解
MySQL数据库案例实战教程:数据类型、语法与高级查询详解
257 3
|
存储 消息中间件 缓存
图解Redis中的9种数据结构(高级面试,必备)
如图所示,Redis中提供了9种不同的数据操作类型,他们分别代表了不同的数据存储结构。
|
1天前
|
存储 JavaScript 前端开发
JavaScript基础
本节讲解JavaScript基础核心知识:涵盖值类型与引用类型区别、typeof检测类型及局限性、===与==差异及应用场景、内置函数与对象、原型链五规则、属性查找机制、instanceof原理,以及this指向和箭头函数中this的绑定时机。重点突出类型判断、原型继承与this机制,助力深入理解JS面向对象机制。(238字)
|
1天前
|
安全 数据可视化 网络安全
安全无小事|阿里云先知众测,为企业筑牢防线
专为企业打造的漏洞信息收集平台
1299 2
|
2天前
|
云安全 人工智能
2025,阿里云安全的“年度报告”
拥抱AI时代,阿里云安全为你护航~
1446 2

热门文章

最新文章