@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"UITabelViewCell";
_listArray = @[@"单元格样式",@"定制单元格背景"];
_tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:_tableView];
//设置数据源
_tableView.dataSource = self;
//设置委托
_tableView.delegate = self;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//获得当前表视图选中的单元格
NSIndexPath *indexPath = [_tableView indexPathForSelectedRow];
//取消当前表视图的选中状态
[_tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - UITableView DataSource
//表视图中存在section的个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//section中包含row的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _listArray.count;
}
//创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIndentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = _listArray[indexPath.row];
return cell;
}
#pragma mark - UITableView delegate
//当用户选择某一行时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetaiViewController *detaiVC = [[DetaiViewController alloc]init];
//获取选择的内容
detaiVC.isBaseCell = indexPath.row ? NO : YES ;
[self.navigationController pushViewController:detaiVC animated:YES];
}
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
@interface DetaiViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{
NSArray *array;
NSMutableArray *tempArray;
NSMutableArray *fontArray;
UITableViewStyle style;
UITableViewCell *cell;
}
@property (nonatomic,assign) BOOL isBaseCell;
@end
@implementation DetaiViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
array = [UIFont familyNames];
if (!self.isBaseCell)
{
self.title = @"Modify Cell BG Type";
style = UITableViewStyleGrouped;
fontArray = [[NSMutableArray alloc]init];
for (int i = 0; i<array.count; i++)
{
//取出字体
NSString *font = array[i];
//将5整除的时候,创建tempArray数据,添加至fontArray中
if (i %5 == 0) {
tempArray = [[NSMutableArray alloc]init];
[fontArray addObject:tempArray];
}
[tempArray addObject:font];
}
}else
{
self.title = @"Cell Base Type";
style = UITableViewStylePlain;
}
UITableView *detaiTV = [[UITableView alloc]initWithFrame:self.view.bounds style:style];
[self.view addSubview:detaiTV];
detaiTV.delegate = self;
detaiTV.dataSource = self;
}
#pragma mark - UITableView DataSource
//表视图中存在section的个数,默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
if (_isBaseCell) {
return 1; //选中第一行时
}else{
return fontArray.count;}
}
//section中包含row的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (_isBaseCell) {
return 4;
}else{
return [fontArray[section] count];}
}
//创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (_isBaseCell) {
//只有一页,无需重用
cell = [[UITableViewCell alloc]initWithStyle:indexPath.row reuseIdentifier:nil];
/* 单元格风格 Style : indexPath.row
* 0 UITableViewCellStyleDefault
* 1 UITableViewCellStyleValue1
* 2 UITableViewCellStyleValue2
* 3 UITableViewCellStyleSubtitle */
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"这是一个默认的风格";
cell.detailTextLabel.text = @"这是副标题";//不显示
cell.imageView.image = [UIImage imageNamed:@"photo"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //箭头
break;
case 1:
cell.textLabel.text = @"这是一个Value1的风格";
cell.detailTextLabel.text = @"这是副标题";
cell.imageView.image = [UIImage imageNamed:@"photo"];
cell.accessoryType = UITableViewCellAccessoryCheckmark; //打勾
break;
case 2:
cell.textLabel.text = @"这是一个Value2的风格";
cell.detailTextLabel.text = @"这是副标题";
cell.imageView.image = [UIImage imageNamed:@"photo"]; //不显示
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; //按钮+箭头
break;
case 3:
cell.textLabel.text = @"这是一个subTitle的风格"; //副标题在下方
cell.detailTextLabel.text = @"这是副标题";
cell.imageView.image = [UIImage imageNamed:@"photo"];
cell.accessoryType = UITableViewCellAccessoryDetailButton;//按钮
break;
default:
break;
}
}else
{
static NSString *cellIndentifier = @"cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [[fontArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
//设置选中背景的风格
// cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//设置背景图片
UIImageView *imageView = [[UIImageView alloc]init];
imageView.image = [UIImage imageNamed:@"bg_balance"];
cell.backgroundView = imageView;
UIImageView *selectionView = [[UIImageView alloc]init];
selectionView.backgroundColor = [UIColor yellowColor];
cell.selectedBackgroundView = selectionView;
}
return cell;
}
//设置section头部视图的title 此方法与自定义的头部视图不能并存
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (_isBaseCell) {
return nil;
}else{
NSString *title = [NSString stringWithFormat:@"第%ld个section",(long)section+1];
return title;}
}
#pragma mark - UITableView Delegate
//设置section头部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (_isBaseCell) {
return 0;
}return 25;
}