#define RandomColor arc4random() %256 /255.0
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//遵守协议
{
NSMutableArray *_dataSource;
}
@property (nonatomic,strong)UITableView *tableView;
#pragma mark - createTableView
- (void)createTableView{
//1.UITableView的创建
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
/*
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // 普通风格:组头组尾停留UITableView的上、下方
UITableViewStyleGrouped // 偏好风格:正常滑动
};
*/
//2.设置背景颜色
self.tableView.backgroundColor = [UIColor cyanColor];
//3.设置背景视图
UIImageView *imageView = [[UIImageViewalloc]initWithFrame:self.view.frame];
imageView.image = [UIImageimageNamed:@"美女.jpg"];
self.tableView.backgroundView = imageView;
//4.设置行高,所有UITableViewCell的行高一样:行高默认44
self.tableView.rowHeight = 60;
//5.设置表头和表尾,只有高度起作用
UIView *headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,0,80)];
headerView.backgroundColor = [UIColorredColor];
self.tableView.tableHeaderView = headerView;
//6.设置分隔线的颜色
self.tableView.separatorColor = [UIColorblackColor];
//7.设置分隔线的类型
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
/*
UITableViewCellSeparatorStyleNone,没有分割线
UITableViewCellSeparatorStyleSingleLine,单线
UITableViewCellSeparatorStyleSingleLineEtched 效果不明显
*/
//8.设置是否可以选中
self.tableView.allowsSelection = YES;
//9.设置是否可以多选
self.tableView.allowsMultipleSelection = YES;
//10.导航栏自动适应表格视图,默认为YES:下移64高度,UITableView继承自UIScrollView
self.automaticallyAdjustsScrollViewInsets =YES;
//11.数据源代理
self.tableView.dataSource =self;
//12.tableView的代理
self.tableView.delegate =self;
[self.viewaddSubview:self.tableView];
//以下属性为Demo外补充属性
//1.tableView预估行高,高度自适应用到的属性
self.tableView.estimatedRowHeight = 50;
//2.设置组索引背景色
self.tableView.sectionIndexBackgroundColor = [UIColor greenColor];
//3.设置组索引上文字颜色
self.tableView.sectionIndexColor = [UIColor blackColor];
//4.设置组索引拖动轨迹背景色
self.tableView.sectionIndexTrackingBackgroundColor = [UIColor lightGrayColor];
//5.设置组头和组尾的高度
self.tableView.sectionFooterHeight = 40;
self.tableView.sectionHeaderHeight = 50;
}
//创建tableView展示的数据
- (void)createDataSource{
_dataSource = [NSMutableArray array];
NSUInteger rand =arc4random() %1000 +1;
for (int i =0; i < rand ; i++) {
[_dataSource addObject:[NSString stringWithFormat:@"又来一位%d",i]];
}
}
#pragma mark - UITableViewDateSourceDelegate 必须实现的两个方法
//section 分组
//row 分组里的行
//根据分组的索引返回该分组中行数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//本例中只有一组数据,直接返回数组的个数
return _dataSource.count;//返回分区里面的行数
}
//通过indexPath,可以得到cell所属的section和 row
//返回指定分区和行的cell,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static int count =0;
//1.第二参数可复用的标识,nil表示创建的cell没有可复用的标识
/*
//如下写法,会一直创建cell。浪费资源,效率低
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
*/
//2、根据可复用标识,从缓冲区中取对应的空闲的cell对象 ,如果不存在,创建新的UITextViewCell
//dequeue 队列 取对象
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIndex"];
if (cell == nil) {
//创建新的UITableViewCell对象,指定的可复用标识必须一样
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIndex"];//可复用的标识
count++;
NSLog(@"%d",count);
}
//3.cell的主标题的文本内容
cell.textLabel.text =_dataSource[indexPath.row];
//4.主标题字体位置
cell.textLabel.textAlignment = NSTextAlignmentCenter;
//5.主标题文本字体颜色
cell.textLabel.textColor = [UIColor grayColor];
//6.cell背景颜色
cell.backgroundColor = [UIColor colorWithRed:RandomColor green:RandomColor blue:RandomColor alpha:0.7];
//7.cell选中后的背景图
cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nav"]];
//8.设置cell左边显示的图像
cell.imageView.image = [UIImage imageNamed:@"thankyou"];
//9.cell右边显示的辅助类型的按钮
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
/*
UITableViewCellAccessoryNone,//没有风格
UITableViewCellAccessoryDisclosureIndicator,//指示风格
UITableViewCellAccessoryDetailDisclosureButton,//详情按钮+指示
UITableViewCellAccessoryCheckmark,//显示对号
UITableViewCellAccessoryDetailButton//指示按钮
*/
//10.cell右边显示的自定义的辅助类型按钮
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"colors"]];
return; cell;
}
#pragma mark - UITableViewDelegate
//根据indexPath设置行高实现该方法后,rowHeight不起作用
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row % 2 ==0) {
return70;
}else{
return40;
}
}
//补充
//1.返回右侧组索引中需要的数组数据
//- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
//2.点击cell的辅助按钮(详情按钮)会触发该方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
//3.选中某行时调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}