#import "RootViewController.h"
#define width [UIScreen mainScreen].bounds.size.width
#define height [UIScreen mainScreen].bounds.size.height
#define topH 64
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
@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<100;i++)
{
[self.rowData addObject:[NSString stringWithFormat:@"Row: %i",i]];
}
[self.tableView reloadData];
}
- (void)scrollToTop:(UIButton *)sender
{
NSIndexPath *topRow = [NSIndexPath 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