#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1??.創(chuàng)建表視圖
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//2??.設(shè)置代理對(duì)象
tableView.dataSource = self;//數(shù)據(jù)源協(xié)議:顯示內(nèi)容
tableView.delegate = self;//代理協(xié)議:表視圖功能
//3??屬性
//1.UIView的所有屬性
//2.分割線顏色
tableView.separatorColor = [UIColor blackColor];
/**
* 3.分割線樣式
UITableViewCellSeparatorStyleNone, 無(wú)分割線
UITableViewCellSeparatorStyleSingleLine, 單線(默認(rèn))
UITableViewCellSeparatorStyleSingleLineEtched Grouped下輔助線
*/
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
//4.設(shè)置所有行高 默認(rèn)44 (寬度是表視圖的寬)
tableView.rowHeight = 100;
//5.設(shè)置頭/尾視圖 (frame 只有高度有效)
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
headerView.backgroundColor = [UIColor yellowColor];
tableView.tableHeaderView = headerView;
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
footerView.backgroundColor = [UIColor yellowColor];
tableView.tableFooterView = footerView;
//6.組頭/尾視圖 高度
tableView.sectionHeaderHeight = 20;
tableView.sectionFooterHeight = 20;
//4??.添加并顯示
[self.view addSubview:tableView];
}
#pragma mark-- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.創(chuàng)建
/**
單元格樣式
UITableViewCellStyleDefault, //text label - image view
UITableViewCellStyleValue1, // 圖 - text - detailText
UITableViewCellStyleValue2, // text (偏右) - detailText (偏左)
UITableViewCellStyleSubtitle // 圖 - text
detailText
*/
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//2.自定義顏色
cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
//圖片
cell.imageView.image = [UIImage imageNamed:@"qqmusic"];
//主標(biāo)題
//IndexPath 表示索引路徑 一個(gè)IndexPath可以代表一個(gè)Cell的位置
cell.textLabel.text = [NSString stringWithFormat:@"%ld-%ld",indexPath.section,indexPath.row];
//副標(biāo)題
cell.detailTextLabel.text = @"detail";
//選中樣式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//選中的背景視圖
// cell.selectedBackgroundView =
//輔助視圖
/**
* UITableViewCellAccessoryNone, 無(wú)
UITableViewCellAccessoryDisclosureIndicator 右向箭頭
UITableViewCellAccessoryDetailDisclosureButton 信息按鈕+右箭頭
UITableViewCellAccessoryCheckmark 對(duì)號(hào)
UITableViewCellAccessoryDetailButton 信息按鈕
*/
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//3.返回
return cell;
}
@end
屏幕快照 2016-03-03 上午9.26.09.png