IOS学习 UITableView 单元格风格和修改单元格背景
来源:互联网 发布:勒索软件下载 编辑:程序博客网 时间:2024/05/03 10:42
@implementation HomeViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.title =@"UITabelViewCell";
_listArray =@[@"单元格样式",@"定制单元格背景"];
_tableView = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
[self.viewaddSubview:_tableView];
//设置数据源
_tableView.dataSource =self;
//设置委托
_tableView.delegate =self;
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//获得当前表视图选中的单元格
NSIndexPath *indexPath = [_tableViewindexPathForSelectedRow];
//取消当前表视图的选中状态
[_tableViewdeselectRowAtIndexPath: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 = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];
}
cell.textLabel.text =_listArray[indexPath.row];
return cell;
}
#pragma mark - UITableView delegate
//当用户选择某一行时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetaiViewController *detaiVC = [[DetaiViewControlleralloc]init];
//获取选择的内容
detaiVC.isBaseCell = indexPath.row ?NO : YES ;
[self.navigationControllerpushViewController: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 {
[superviewDidLoad];
// Do any additional setup after loading the view.
array = [UIFontfamilyNames];
if (!self.isBaseCell)
{
self.title =@"Modify Cell BG Type";
style =UITableViewStyleGrouped;
fontArray = [[NSMutableArrayalloc]init];
for (int i =0; i<array.count; i++)
{
//取出字体
NSString *font = array[i];
//将5整除的时候,创建tempArray数据,添加至fontArray中
if (i %5 ==0) {
tempArray = [[NSMutableArrayalloc]init];
[fontArray addObject:tempArray];
}
[tempArray addObject:font];
}
}else
{
self.title =@"Cell Base Type";
style =UITableViewStylePlain;
}
UITableView *detaiTV = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:style];
[self.viewaddSubview: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 = [[UITableViewCellalloc]initWithStyle:indexPath.rowreuseIdentifier: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 = [UIImageimageNamed:@"photo"];
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator; //箭头
break;
case 1:
cell.textLabel.text =@"这是一个Value1的风格";
cell.detailTextLabel.text =@"这是副标题";
cell.imageView.image = [UIImageimageNamed:@"photo"];
cell.accessoryType =UITableViewCellAccessoryCheckmark; //打勾
break;
case 2:
cell.textLabel.text =@"这是一个Value2的风格";
cell.detailTextLabel.text =@"这是副标题";
cell.imageView.image = [UIImageimageNamed:@"photo"]; //不显示
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton; //按钮+箭头
break;
case 3:
cell.textLabel.text =@"这是一个subTitle的风格"; //副标题在下方
cell.detailTextLabel.text =@"这是副标题";
cell.imageView.image = [UIImageimageNamed:@"photo"];
cell.accessoryType =UITableViewCellAccessoryDetailButton;//按钮
break;
default:
break;
}
}else
{
static NSString *cellIndentifier =@"cell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell ==nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIndentifier];
}
cell.textLabel.text = [[fontArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
//设置选中背景的风格
// cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//设置背景图片
UIImageView *imageView = [[UIImageViewalloc]init];
imageView.image = [UIImageimageNamed:@"bg_balance"];
cell.backgroundView = imageView;
UIImageView *selectionView = [[UIImageViewalloc]init];
selectionView.backgroundColor = [UIColoryellowColor];
cell.selectedBackgroundView = selectionView;
}
return cell;
}
//设置section头部视图的title 此方法与自定义的头部视图不能并存
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if (_isBaseCell) {
return nil;
}else{
NSString *title = [NSStringstringWithFormat:@"第%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;
}
- IOS学习 UITableView 单元格风格和修改单元格背景
- IOS iPhone 开发 UItableView中的单元格背景渐变
- [swift] UITableView单元格背景透明问题
- 【ios学习记录】-如何定制UITableView的圆角单元格
- iOS学习笔记-030.UITableView——自定义单元格UITableViewCell
- ios UITableView单元格多选框的实现
- [ios]设置表格单元格交替背景 【转】
- UITableView 颜色交替单元格。
- 定制UITableView的单元格
- UITableView中的单元格重用
- UITableView的“静态单元格”
- 动态修改DataGridView单元格的类型和编辑单元格
- 改变单元格背景
- html单元格背景
- iOS UItableView+UINavigationController实现单元格的删除
- iOS UItableView+UINavigationController实现单元格的删除
- iOS之UITableView的使用——处理选中单元格
- iOS之UITableView的使用——静态单元格
- bitmap排序
- codeforces 607 A Chain Reaction (二分)
- 下列数据结构具有记忆功能的是
- BZOJ 3995 SDOI2015 道路修建
- H264参数语法文档: SPS、PPS、IDR
- IOS学习 UITableView 单元格风格和修改单元格背景
- NVIDIA Jetson TK1安装opencv
- Sublime Text 2搭建Go开发环境(Windows)
- $.ajax()post方式请求参数无法传递,request.getParameter()无法获取
- 面试C++中的常见问题
- $.ajax contenType是appliation/json的时候,spring mvc后台无法接受data参数
- 任务与函数的声明与调用
- 写Java代码分别使堆溢出,栈溢出
- Linux的常用目录及寒假学习笔记