IOS学习 UITableView 单元格风格和修改单元格背景
2016-03-10 21:47
197 查看
@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;
}
- (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;
}
相关文章推荐
- $.ajax()post方式请求参数无法传递,request.getParameter()无法获取
- 优先队列priority_queue基本操作
- Handler,Looper, MessageQueue的工作原理解析
- Arduino IDE的编译执行过程解读
- 关于在UI中刷新主线程的问题
- 简单使用NSURLConnection、NSURLRequest和NSURL
- iOSUI简单的页面轮转
- PAT-Talent and Virtue (25)
- 百度Ueditor编辑器的Html模式自动替换样式的解决方法
- requirejs 第一个实例
- bzoj 2656 [Zjoi2012]数列(sequence)(高精度)
- Arduino关于旋转编码器程序的介绍(Reading Rotary Encoders)
- mysql起动报错The server quit without updating PID file /usr/local/mysql/data/
- 并查集-poj2524-Ubiquitous Religions
- uiatuomator如何调试
- WPF:如何在工作线程中更新窗体的UI元素(Dispatcher机制)
- UVa 11039 Building designing (求按绝对值升序最长正负交替数列长度)
- uiatuomator命令启动apk,与查找多个相同控件
- ANSI/VT100 Terminal Control Escape Sequences
- uiautomator日志文件转换为xml格式文件