IOS 动态改变cell的高度
来源:互联网 发布:洛阳大数据产业园 编辑:程序博客网 时间:2024/05/01 21:06
#import "ViewController.h"
@interface ViewController ()
{
UITableView *_table;
NSMutableArray *_dataList;
UIView *footView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_table.delegate=self;
_table.dataSource=self;
//_table.multipleTouchEnabled=YES;
[self.view addSubview:_table];
_dataList=[[NSMutableArray alloc]initWithObjects:@"1111\n111\n111\n111\n",@"222",@"333",@"444",@"555",@"666",@"777", nil];
footView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
footView.backgroundColor=[UIColor yellowColor];
_table.tableFooterView=footView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.tag = 1;
//label.lineBreakMode = UILineBreakModeCharacterWrap;
label.lineBreakMode=NSLineBreakByCharWrapping;
label.highlightedTextColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell viewWithTag:1];
NSString *text;
text = [_dataList objectAtIndex:indexPath.row];
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);
label.text = text;
CGRect rect = CGRectInset(cellFrame, 2, 2);
label.frame = rect;
[label sizeToFit];
if (label.frame.size.height > 46) {
cellFrame.size.height = 50 + label.frame.size.height - 46;
}
else {
cellFrame.size.height = 50;
}
[cell setFrame:cellFrame];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
@end
@interface ViewController ()
{
UITableView *_table;
NSMutableArray *_dataList;
UIView *footView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_table=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_table.delegate=self;
_table.dataSource=self;
//_table.multipleTouchEnabled=YES;
[self.view addSubview:_table];
_dataList=[[NSMutableArray alloc]initWithObjects:@"1111\n111\n111\n111\n",@"222",@"333",@"444",@"555",@"666",@"777", nil];
footView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
footView.backgroundColor=[UIColor yellowColor];
_table.tableFooterView=footView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_dataList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.tag = 1;
//label.lineBreakMode = UILineBreakModeCharacterWrap;
label.lineBreakMode=NSLineBreakByCharWrapping;
label.highlightedTextColor = [UIColor whiteColor];
label.numberOfLines = 0;
label.opaque = NO; // 选中Opaque表示视图后面的任何内容都不应该绘制
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
}
UILabel *label = (UILabel *)[cell viewWithTag:1];
NSString *text;
text = [_dataList objectAtIndex:indexPath.row];
CGRect cellFrame = [cell frame];
cellFrame.origin = CGPointMake(0, 0);
label.text = text;
CGRect rect = CGRectInset(cellFrame, 2, 2);
label.frame = rect;
[label sizeToFit];
if (label.frame.size.height > 46) {
cellFrame.size.height = 50 + label.frame.size.height - 46;
}
else {
cellFrame.size.height = 50;
}
[cell setFrame:cellFrame];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
return cell.frame.size.height;
}
@end
效果是这样的
- IOS 动态改变cell的高度
- IOS 动态改变cell的高度
- iOS 动态改变cell的高度
- 动态改变CELL 的高度
- 动态改变tableview的cell的高度
- IOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- UITableView动态改变Cell高度
- UITableView动态改变Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- 【iOS知识学习】_iOS动态改变TableView Cell高度
- iOS开发-UI控件:动态改变UITableView中的Cell高度
- ios开发笔记之如何让tableView根据文本内容动态改变cell的高度
- 点击cell动态改变cell高度
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- iOS开发动态计算cell的高度
- Oracle小知识点
- CSDN 的这句话是神马意思?
- mysql的查询语句
- python输入输出
- 存储过程
- IOS 动态改变cell的高度
- 学习用js, ajax, php做一个简单的小黄鸡页面(调用simsimi API)
- 基于 libmad 的简单 MP3 流媒体播放器的实现
- Omnet错误:the module is not linked with Define_Module()
- PL/SQL:有关FORALL的用法与FOR,插入记录对比
- Oracle 创建dblink 报错:ORA-01017、ORA-02063
- SWFUpload V2.2.0 说明文档
- unresolved external symbol __endthreadex错误解决
- javah