#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

[super viewDidLoad];

self.data = [NSMutableArray arrayWithArray:[UIFont familyNames]];

//设置表视图分割线风格没有分割线

//_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

//没有分割线

//_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

//设置分割线颜色,默认为灰色

_tableView.separatorColor = [UIColor orangeColor];

//设置分割线的偏移量

//_tableView.separatorInset = UIEdgeInsetsMake(0, -50, 0, -50);

//设置表视图的头部视图

UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 160)];

//创建按钮

UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];

btn.frame = CGRectMake(100, 50, 100, 100);

[headerView addSubview:btn];

[btn addTarget:self action:@selector(btn_click:) forControlEvents:UIControlEventTouchUpInside];

headerView.backgroundColor = [UIColor yellowColor];

_tableView.tableHeaderView = headerView;

//设置表视图的尾部视图

UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];

footView.backgroundColor = [UIColor purpleColor];

_tableView.tableFooterView = footView;

//设置单元格的行高,默认为44,如果要动态的设置行高,需要在代理方法中实现

//_tableView.rowHeight = 100;

}


- (void)btn_click:(UIButton *)btn{

//点击按钮删除第一个元素

[self.data removeObjectAtIndex:0];

//刷新表视图,会重新执行数据源方法和代理方法

//[self.tableView reloadData];

//把第五个元素改为华文琥珀

//构建IndexPath

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:5 inSection:0];

//取到单元格

UITableViewCell *tvc = [self.tableView cellForRowAtIndexPath:indexPath];

tvc.textLabel.text = @"华文琥珀";

// //输出在屏幕上显示的单元格的内容

// NSArray *cells = [self.tableView visibleCells];

// for (UITableViewCell *cell in cells) {

// NSLog(@"cell is %@",cell.textLabel.text);

// }

//

// //输出在屏幕上显示的单元格的下标

// NSArray *indexPaths = [self.tableView indexPathsForVisibleRows];

// for (NSIndexPath *indexPath in indexPaths) {

// NSLog(@"indexPath is %ld",indexPath.row);

// }

// //输出选中的单元格

// NSArray *selecedRows = [self.tableView indexPathsForSelectedRows];

// for (NSIndexPath *selectedRow in selecedRows) {

// NSLog(@"%@",selectedRow);

// }

//滑动到指定位置,可配置动画

NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:10 inSection:0];

[self.tableView scrollToRowAtIndexPath:indexPath2 atScrollPosition:UITableViewScrollPositionTop animated:YES];


}


#pragma mark--UITableViewDataSource

//表视图的行数

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

return self.data.count;

}


//单元格内容

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

UITableViewCell *tvCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

NSString *str = self.data[indexPath.row];

tvCell.textLabel.text = str;

return tvCell;

}

//设置单元格的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row == 3) {

return 80;

}else if (indexPath.row == 5){

return 100;

}

return 40;//无效,不会执行

}



- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@end