UITableView UITableViewCell
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
1、创建cell时,不从重用池找,进来就创建
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1.自定义一个继承uitableviewcell的类
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subLabel;
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_newsImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 100, 80)];
[self addSubview:_newsImg];
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(115, 5, 200, 30)];
[self addSubview:_titleLabel];
_subLabel = [[UILabel alloc] initWithFrame:CGRectMake(115, 55, 200, 30)];
[self addSubview:_subLabel];
}
return self;
#import "NewsInfo.h"
#import "MyTableViewCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
NSArray *_array;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
NewsInfo *info = [[NewsInfo alloc] init];
info.imageName = @"1.jpg";
info.title = @"马寨拆迁";
info.subTitle = @"马寨拆迁,热火朝天";
NewsInfo *info1 = [[NewsInfo alloc] init];
info1.imageName = @"2.jpg";
info1.title = @"大马寨拆迁";
info1.subTitle = @"马寨拆迁,热火朝天";
_array = @[info,info1];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
NewsInfo *_info = _array[indexPath.row];
cell.newsImg.image = [UIImage imageNamed:_info.imageName];
cell.titleLabel.text = _info.title;
cell.subLabel.text = _info.subTitle;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 90;
@property (nonatomic, copy) NSString *title;
cell.newsImg.image = [UIImage imageNamed:_info.imageName];
cell.titleLabel.text = _info.title;
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//1。创建 两种格式UITableViewStylePlain和UITableViewStyleGrouped,其本质上没多大区别,主要是界面风格
//2。行高
table.rowHeight = 100;
//3设置分割线的样式风格//很多种自己选择
table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//4设置分割线的颜色
table.separatorColor = [UIColor blackColor];
//5给table添加背景 可以不设置背景的frame
//5.1第一种: UIView *view =[[UIView alloc]init];
//view.backgroundColor = [UIColor grayColor];
//5.2第二种
UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
table.backgroundView = view;
//6设置列表的头视图,可以是view或者其子类的对象//顶部视图,其frame只有高有效
UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
view2.backgroundColor = [UIColor clearColor];
table.tableHeaderView = view2;
//7设置代理
table.delegate =self;
//8设置数据源
table.dataSource = self;
table.tableFooterView = [[UIView alloc]init];//的作用是只显示自己定义的行数
[self.view addSubview:table];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 5;//table的组数,默认是一组
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return 1;
}else if (section ==1){
return 5;
}else if(section ==2){
return 6;
}
return 4;//设置每组cell的行数
}
//设置每行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// indexpath包含了第几组:indexpath.section 和第几行:indexpath.row
static NSString *identifier = @"cell";//重用机制标识
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];//根据重用标识,到重用池找到对应的cell
if (cell ==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle//改变style后面的风格样式会//
//cell的四种样式
//UITableViewCellStyleDefault,只显示图片和标题
//UITableViewCellStyleValue1,显示图片,标题和子标题(子标题在右边)
//UITableViewCellStyleValue2,标题和子标题
//UITableViewCellStyleSubtitle显示图片,标题和子标题(子标题在下边)
reuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识
}
cell.backgroundColor = [UIColor clearColor];//cell的背景颜色
cell.textLabel.text = [NSString stringWithFormat:@"第%zi组 第%zi行",indexPath.section,indexPath.row];//设置cell的文本信息标题以及副标题
//cell.imageView.image = [UIImage imageNamed:@"012"
//];//给每组每行添加图片
cell.detailTextLabel.text =@ "who are you";//设置子标题
if (indexPath.section==0 &&indexPath.row==0) {
cell.imageView.image = [UIImage imageNamed:@"21"];
}
if (indexPath.section ==1 && indexPath.row ==2) {
cell.imageView.image =[UIImage imageNamed:@"2"];
}//向第几组,第几行加图片。
//1 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//点击cell出现>箭头 点击之后跳至下级界面
//2 cell.accessoryType = UITableViewCellAccessoryNone;//没有配件 默认是没有配件
//3 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点 点击按钮(叹号)会有相关提示,点击cell后会跳转到下级界面 在这按钮时>符号也会出现
//4cell.accessoryType = UITableViewCellAccessoryDetailButton;//会出现叹号(i)点击之后会有相关提示。
//5 cell.accessoryType =UITableViewCellAccessoryCheckmark;//显示对勾,是一个简单的配件
return cell;//将设置好的cell返回
}
//自定义每组头视图
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;//设置每组顶部视图的高度即把每组分割开(return的是视图的高度
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor whiteColor];
return view;//自定义每组头视图
}
//自定义尾视图的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
//自定义尾部视图的颜色等等。
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor whiteColor];
return view;
}
//设置每组第几行的高度适用于全部所选每组的行
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==0) {
return 100;
}
return 30;
}
//在每组相隔中间显示下边是第几组 前提是没有自定义顶视图和尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSLog(@"%zi",section);
return [NSString stringWithFormat:@"%zi",section];
}
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
UITableView *_tableView;
NSMutableArray *_array;//盛放要显示的东西
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_array = [NSMutableArray arrayWithObjects:@"111",@"222",@"333", nil];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 647) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor grayColor];
button.frame = CGRectMake(300, 20, 60, 60);
[button setTitle:@"+" forState:UIControlStateNormal];
[button addTarget:self action:@selector(addCell) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addCell{
NSLog(@"=====");
[_array addObject:@"123"];
[_tableView reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// cell.selectionStyle = UITableViewCellSelectionStyleDefault;//cell选中的背景色
UIView *view = [[UIView alloc] initWithFrame:cell.frame];
view.backgroundColor = [UIColor blueColor];
cell.selectedBackgroundView = view;//自定义cell选中背景
cell.textLabel.text = _array[indexPath.row];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
// cell.accessoryView 自定义右侧视图
return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"----%zi",indexPath.row);
}
NSLog(@"----%zi",indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];//当cell被点击时,取消选中状态
}
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//提交修改动作: 再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[_array removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];//删除行
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
table.dataSource = self;
[self.view addSubview:table];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1 NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//2 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = nil;//3 先清理,在使用
if (indexPath.row == 1) {
cell.textLabel.text = @"1212";
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//2。行高
table.rowHeight = 100;
//3设置分割线的样式风格//很多种自己选择
table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//4设置分割线的颜色
table.separatorColor = [UIColor blackColor];
//5给table添加背景 可以不设置背景的frame
//5.1第一种: UIView *view =[[UIView alloc]init];
//view.backgroundColor = [UIColor grayColor];
//5.2第二种
UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
table.backgroundView = view;
//6设置列表的头视图,可以是view或者其子类的对象//顶部视图,其frame只有高有效
UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
view2.backgroundColor = [UIColor clearColor];
table.tableHeaderView = view2;
//7设置代理
table.delegate =self;
//8设置数据源
table.dataSource = self;
table.tableFooterView = [[UIView alloc]init];//的作用是只显示自己定义的行数
[self.view addSubview:table];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 5;//table的组数,默认是一组
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return 1;
}else if (section ==1){
return 5;
}else if(section ==2){
return 6;
}
return 4;//设置每组cell的行数
}
//设置每行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// indexpath包含了第几组:indexpath.section 和第几行:indexpath.row
static NSString *identifier = @"cell";//重用机制标识
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];//根据重用标识,到重用池找到对应的cell
if (cell ==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle//改变style后面的风格样式会//
//cell的四种样式
//UITableViewCellStyleDefault,只显示图片和标题
//UITableViewCellStyleValue1,显示图片,标题和子标题(子标题在右边)
//UITableViewCellStyleValue2,标题和子标题
//UITableViewCellStyleSubtitle显示图片,标题和子标题(子标题在下边)
reuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识
}
cell.backgroundColor = [UIColor clearColor];//cell的背景颜色
cell.textLabel.text = [NSString stringWithFormat:@"第%zi组 第%zi行",indexPath.section,indexPath.row];//设置cell的文本信息标题以及副标题
//cell.imageView.image = [UIImage imageNamed:@"012"
//];//给每组每行添加图片
cell.detailTextLabel.text =@ "who are you";//设置子标题
if (indexPath.section==0 &&indexPath.row==0) {
cell.imageView.image = [UIImage imageNamed:@"21"];
}
if (indexPath.section ==1 && indexPath.row ==2) {
cell.imageView.image =[UIImage imageNamed:@"2"];
}//向第几组,第几行加图片。
//1 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//点击cell出现>箭头 点击之后跳至下级界面
//2 cell.accessoryType = UITableViewCellAccessoryNone;//没有配件 默认是没有配件
//3 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点 点击按钮(叹号)会有相关提示,点击cell后会跳转到下级界面 在这按钮时>符号也会出现
//4cell.accessoryType = UITableViewCellAccessoryDetailButton;//会出现叹号(i)点击之后会有相关提示。
//5 cell.accessoryType =UITableViewCellAccessoryCheckmark;//显示对勾,是一个简单的配件
return cell;//将设置好的cell返回
}
//自定义每组头视图
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;//设置每组顶部视图的高度即把每组分割开(return的是视图的高度
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor whiteColor];
return view;//自定义每组头视图
}
//自定义尾视图的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
//自定义尾部视图的颜色等等。
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor whiteColor];
return view;
}
//设置每组第几行的高度适用于全部所选每组的行
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==0) {
return 100;
}
return 30;
}
//在每组相隔中间显示下边是第几组 前提是没有自定义顶视图和尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSLog(@"%zi",section);
return [NSString stringWithFormat:@"%zi",section];
}
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
UITableView *_tableView;
NSMutableArray *_array;//盛放要显示的东西
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_array = [NSMutableArray arrayWithObjects:@"111",@"222",@"333", nil];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 647) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor grayColor];
button.frame = CGRectMake(300, 20, 60, 60);
[button setTitle:@"+" forState:UIControlStateNormal];
[button addTarget:self action:@selector(addCell) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addCell{
NSLog(@"=====");
[_array addObject:@"123"];
[_tableView reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// cell.selectionStyle = UITableViewCellSelectionStyleDefault;//cell选中的背景色
UIView *view = [[UIView alloc] initWithFrame:cell.frame];
view.backgroundColor = [UIColor blueColor];
cell.selectedBackgroundView = view;//自定义cell选中背景
cell.textLabel.text = _array[indexPath.row];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
// cell.accessoryView 自定义右侧视图
return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"----%zi",indexPath.row);
}
NSLog(@"----%zi",indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];//当cell被点击时,取消选中状态
}
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//提交修改动作: 再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[_array removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];//删除行
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
table.dataSource = self;
[self.view addSubview:table];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1 NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//2 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = nil;//3 先清理,在使用
if (indexPath.row == 1) {
cell.textLabel.text = @"1212";
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
UITableView *table = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
//2。行高
table.rowHeight = 100;
//3设置分割线的样式风格//很多种自己选择
table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//4设置分割线的颜色
table.separatorColor = [UIColor blackColor];
//5给table添加背景 可以不设置背景的frame
//5.1第一种: UIView *view =[[UIView alloc]init];
//view.backgroundColor = [UIColor grayColor];
//5.2第二种
UIImageView *view = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1"]];
table.backgroundView = view;
//6设置列表的头视图,可以是view或者其子类的对象//顶部视图,其frame只有高有效
UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
view2.backgroundColor = [UIColor clearColor];
table.tableHeaderView = view2;
//7设置代理
table.delegate =self;
//8设置数据源
table.dataSource = self;
table.tableFooterView = [[UIView alloc]init];//的作用是只显示自己定义的行数
[self.view addSubview:table];
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 5;//table的组数,默认是一组
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return 1;
}else if (section ==1){
return 5;
}else if(section ==2){
return 6;
}
return 4;//设置每组cell的行数
}
//设置每行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// indexpath包含了第几组:indexpath.section 和第几行:indexpath.row
static NSString *identifier = @"cell";//重用机制标识
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier];//根据重用标识,到重用池找到对应的cell
if (cell ==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle//改变style后面的风格样式会//
//cell的四种样式
//UITableViewCellStyleDefault,只显示图片和标题
//UITableViewCellStyleValue1,显示图片,标题和子标题(子标题在右边)
//UITableViewCellStyleValue2,标题和子标题
//UITableViewCellStyleSubtitle显示图片,标题和子标题(子标题在下边)
reuseIdentifier:identifier];//创建一个cell,设置其样式以及其标识
}
cell.backgroundColor = [UIColor clearColor];//cell的背景颜色
cell.textLabel.text = [NSString stringWithFormat:@"第%zi组 第%zi行",indexPath.section,indexPath.row];//设置cell的文本信息标题以及副标题
//cell.imageView.image = [UIImage imageNamed:@"012"
//];//给每组每行添加图片
cell.detailTextLabel.text =@ "who are you";//设置子标题
if (indexPath.section==0 &&indexPath.row==0) {
cell.imageView.image = [UIImage imageNamed:@"21"];
}
if (indexPath.section ==1 && indexPath.row ==2) {
cell.imageView.image =[UIImage imageNamed:@"2"];
}//向第几组,第几行加图片。
//1 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//点击cell出现>箭头 点击之后跳至下级界面
//2 cell.accessoryType = UITableViewCellAccessoryNone;//没有配件 默认是没有配件
//3 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//提示用户可点 点击按钮(叹号)会有相关提示,点击cell后会跳转到下级界面 在这按钮时>符号也会出现
//4cell.accessoryType = UITableViewCellAccessoryDetailButton;//会出现叹号(i)点击之后会有相关提示。
//5 cell.accessoryType =UITableViewCellAccessoryCheckmark;//显示对勾,是一个简单的配件
return cell;//将设置好的cell返回
}
//自定义每组头视图
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;//设置每组顶部视图的高度即把每组分割开(return的是视图的高度
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor whiteColor];
return view;//自定义每组头视图
}
//自定义尾视图的高
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
//自定义尾部视图的颜色等等。
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor whiteColor];
return view;
}
//设置每组第几行的高度适用于全部所选每组的行
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row ==0) {
return 100;
}
return 30;
}
//在每组相隔中间显示下边是第几组 前提是没有自定义顶视图和尾视图
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSLog(@"%zi",section);
return [NSString stringWithFormat:@"%zi",section];
}
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>{
UITableView *_tableView;
NSMutableArray *_array;//盛放要显示的东西
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_array = [NSMutableArray arrayWithObjects:@"111",@"222",@"333", nil];
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 647) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor grayColor];
button.frame = CGRectMake(300, 20, 60, 60);
[button setTitle:@"+" forState:UIControlStateNormal];
[button addTarget:self action:@selector(addCell) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
//点击button,tableview的Cell个数+1
-(void)addCell{
NSLog(@"=====");
[_array addObject:@"123"];
[_tableView reloadData];//当tableView的数据源发生改变时,调用该方法,会更新tableView的显示,
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// cell.selectionStyle = UITableViewCellSelectionStyleDefault;//cell选中的背景色
UIView *view = [[UIView alloc] initWithFrame:cell.frame];
view.backgroundColor = [UIColor blueColor];
cell.selectedBackgroundView = view;//自定义cell选中背景
cell.textLabel.text = _array[indexPath.row];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];//选中状态下,字体颜色
// cell.accessoryView 自定义右侧视图
return cell;
}
//当cell的accessoryStyle中包含信息按钮(叹号)时,点击按钮触发的方法
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
}
//取消选中
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"----%zi",indexPath.row);
}
NSLog(@"----%zi",indexPath.row);
[tableView deselectRowAtIndexPath:indexPath animated:YES];//当cell被点击时,取消选中状态
}
//是否允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//提交修改动作: 再执行删除之前,需要先移除数组中对应的元素,
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
[_array removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];//删除行
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@interface ViewController ()<UITableViewDataSource>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
table.dataSource = self;
[self.view addSubview:table];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1 NSString *identifier = [NSString stringWithFormat:@"cell%zi%zi",indexPath.section,indexPath.row];//给每行设置不同的标识
static NSString *identifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//2 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];处理重用bug很不友好的方式,不建议使用
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = nil;//3 先清理,在使用
if (indexPath.row == 1) {
cell.textLabel.text = @"1212";
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
UITableView UITableViewCell的更多相关文章
- iOS:UITableView相关(18-10-20更)
UITableView用得较多,遇到的情况也较多,单独记录一篇. 一.零散的技巧 二.取cell 三.cell高度 四.导航栏.TableView常见问题相关 五.自定义左滑删除按钮图片 六.仅做了解 ...
- 关于使用uitableview 中cell 来实现uiimageview的复用和图片的异步加载
apple sample lazytableimages 1,首先设置横向显示的uitableview self.customTableview.transform = CGAffineTransfo ...
- UITableViewController
UITableViewController 目录 概述 UITableView UITableViewCell 与UITableViewController相关的代理 UITableViewDataS ...
- 在iOS中获取UIView的所有层级结构 相关
在iOS中获取UIView的所有层级结构 应用场景 在实际 iOS 开发中,很多时候都需要知道某个 UI 控件中包含哪些子控件,并且分清楚它们的层级结构和自个的 frame 以及 bounds ,以便 ...
- iOS - Xcode项目统计总代码行数
最新公司需要把项目代码量统计一下,第一时间找到Xcode插件管理工具Alcatraz,查找插件ZLXCodeLine,这是一个快速统计Xcode工程项目代码量的插件,好像已经不支持Alcatraz安装 ...
- UITableViewCell和UITableView的学习
一:自定义UITableViewCell: 先来看UITableView.h: - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSS ...
- iOS UITableView , UITableViewController ,UITableViewCell实现全国各省市遍历,选择相应的地区
我们先看一下效果 代码如下 首先是第一个页面 rootTableViewController.h #import <UIKit/UIKit.h> #im ...
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不执行的问题
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...
- UITableView 的增删改 自定义UITableViewCell
1.UITableView的增删改 //设置编辑模式 [self.tableView setEditing:YES animated:YES]; //可以不写 - (BOOL)tableView:(U ...
随机推荐
- 题目描述: k一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
时间限制:1秒 空间限制:32768k 斐波那契数列指的是这样一个数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,9 ...
- OC-@property、self及类的本质
让代码书写更加简便 --1-- 设置器和访问器 1.1 setter 1.2 getter --2-- 类的本质 2.1 类类型的对象 2.2 类的本质 2.3 如何获取类对象 2.4 类对象的使用 ...
- 用eclipse打开已经编译的工程
第一种方法: eciplise------>File------>Import------>General------>Existing Project into Worksp ...
- Eclipse 代码格式:“{ }”的格式设置
Eclipse设置代码大括号的格式 编写代码有很多中风格,常见的三种风格: 1.K & R风格:这种风格的代码比较紧凑,优点是在教科书或者打印成纸张的时候比较省地方:缺点是大括号匹配问题,代码 ...
- Java进行post和get传参数
http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html get和post方法 import java.io.BufferedRe ...
- linux screen 命令详解
一.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...
- linux和android博客链接
1.Tracy Mcgrady的专栏冰山一角:linux和Android底层开发,主要是mtk系列点击打开链接 2.郁闷Wednesday:嵌入式linux 单片机 android,点击打开链接 3. ...
- Django 1.10 找不到静态资源解决方法
测试版本:Django 1.10 问题:Django项目找不到静态资源 解决方法: 1.首先你需要在自己的app下面创建2个目录 static 和 templates 树形结构如下(DjangoPr ...
- DuiLib 源码分析之CDuiString
duilib是一个比较常见的界面库,闲来无事看看别人写的代码,跟自己写的一比, 才看到了差距呀,感觉自己写的乱七八糟,keep moving CduiString是duilib提供的一个字符串类,功能 ...
- Java动态代理与Cglib库
JDK动态代理 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在 ...