UITableViewCell的 Cell 内的图片自适应高度

  1 #import "MainViewController.h"
  2
  3 #import "MyCell.h"
  4
  5 @interface MainViewController ()<UITableViewDelegate, UITableViewDataSource>;
  6
  7
  8
  9 @end
 10
 11
 12 @implementation MainViewController
 13
 14
 15 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 16
 17 {
 18
 19     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
 20
 21     if (self) {
 22
 23         // Custom initialization
 24
 25
 26
 27         self.array = [NSMutableArray array];
 28
 29
 30
 31         for (int i = 0; i < 26; i++) {
 32
 33             UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];
 34
 35             [self.array addObject:image];
 36
 37         }
 38
 39     }
 40
 41     return self;
 42
 43 }
 44
 45
 46 - (void)viewDidLoad
 47
 48 {
 49
 50     [super viewDidLoad];
 51
 52     // Do any additional setup after loading the view.
 53
 54
 55
 56     self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)style:UITableViewStylePlain];
 57
 58     self.tableView.delegate = self;
 59
 60     self.tableView.dataSource = self;
 61
 62
 63
 64     [self.view addSubview:self.tableView];
 65
 66     [self.tableView release];
 67
 68 }
 69
 70
 71 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
 72
 73 {
 74
 75     // 在tableView的协议方法中制定 每个cell的高度
 76
 77
 78
 79     UIImage *image = [self.array objectAtIndex:indexPath.row];
 80
 81
 82
 83     // cell的高度 = cell的宽度 * 图片的高度 / 图片的宽度
 84
 85     CGFloat cellHeight = tableView.bounds.size.width * image.size.height / image.size.width;
 86
 87     return cellHeight;
 88
 89
 90
 91 }
 92
 93
 94 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 95
 96 {
 97
 98     NSLog(@"%d", self.array.count);
 99
100     return [self.array count];
101
102 }
103
104 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
105
106 {
107
108     static NSString *cellID = @"Reuse";
109
110     MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
111
112     if (cell == nil) {
113
114         cell = [[[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]autorelease];
115
116     }
117
118     UIImage *image = [self.array objectAtIndex:indexPath.row];
119
120     cell.myImageView.image = image;
121
122     return cell;
123
124 }
125
126 - (void)didReceiveMemoryWarning
127
128 {
129
130     [super didReceiveMemoryWarning];
131
132     // Dispose of any resources that can be recreated.
133
134 }
时间: 2024-08-29 18:30:29

UITableViewCell的 Cell 内的图片自适应高度的相关文章

文本图片自适应高度小bug以及解决办法

自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中 点击效果: 注:- (void)layoutSubviews 方法不能同时操作,否则会出现cell的高度错乱 显示: 点击:

css利用padding百分比实现图片自适应高度

应用场景 宽高比率,实现图片自适应高度,防止图片加载过程高度为0,加载完图片高度撑起,它下面的div抖动问题 重点:CSS百分比padding都是相对宽度计算的 <div class="works-item-t"> <img src="./150x200.png"> </div> .works-item-t { padding-bottom: 133%; position: relative; } .works-item-t &g

UIView的自适应高度 (图像,文字)

1 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 2 3 { 4 5 // 在tableView的协议方法中制定 每个cell的高度 6 7 8 9 UIImage *image = [self.array objectAtIndex:indexPath.row]; 10 11 12 13 // cell的高度 = cell的宽度 * 图片的高度 / 图

怎样让自定义Cell的图片和文本自适应高度

Let's do it! 首先创建一个Model类 包括一个图片名称属性 还有文字内容属性 #import <Foundation/Foundation.h> @interface Model : NSObject @property (nonatomic, copy) NSString *imageName; @property (nonatomic, copy) NSString *info; @end 创建一个继承于UITableViewCell的MyTableViewCell,把模型作

TableView cell自适应高度-----xib

1.通过xib创建一个cell,将label进行上左下右,进行适配, self.automaticallyAdjustsScrollViewInsets = NO; self.edgesForExtendedLayout = UIRectEdgeNone;//将原点移动到navigationBar下面去了 tableView.estimatedRowHeight = 37.0;//估计cell的高度 tableView.rowHeight = UITableViewAutomaticDimens

第?一讲:UITableView 高级 自定义cell , cell自适应高度

一.自定义cell(包括cell的自定义,以及直接赋值的方法) 自定义cell就是创建一个UITableViewCell的子类. 把cell上的控件创建都封装在子类中,简化UIViewController中的代码 示例代码分析:(这个例子包括cell的自定义,以及直接赋值的方法) 1.需要建立tabelViewCell类, 在其中进行cell上控件的添加 2.在tabelViewCell.m中进行初始化,和layoutSubviews的frame布局的操作 tabelViewCell.h定义属性

【转】UITableViewCell自适应高度 UILabel自适应高度和自动换行

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    // 列寬    CGFloat contentWidth = self.tableView.frame.size.width;    // 用何種字體進行顯示    UIFont *font = [UIFont systemFontOfSize:13];       // 該行要顯示的內容    

自定义 cell 自适应高度

#import "CommodityCell.h" #import "UIImageView+WebCache.h" @implementation CommodityCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:re

转:自定义UITableViewCell:Cell高度、分割线、间距等

UITableView的强大更多程度上来自于可以任意自定义UITableViewCell单元格. 通常,UITableView中的Cell是 动态的,在使用过程中,会创建一个Cell池,根据每个cell的高度(即tableView:heightForRowAtIndexPath:返回 值),以及屏幕高度计算屏幕中可显示几个cell.而进行自定义TableViewCell无非是采用代码实现或采用IB编辑nib文件来实现两种方式, 本文主要收集代码的方式实现各种cell自定义. 如何动态调整Cell