zoukankan      html  css  js  c++  java
  • 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 }
    有人说:爱上一座城,是因为城里住着某个人,能够与所爱的人在一起,连光阴都是美的。即便粗茶淡饭,修篱种田,只要有你陪伴就好。那么,找一个青山绿水的地方,寻一处幽静的茅舍,或是云水禅心的庭院,那里有晴朗的阳光和静谧的悠然,还有你明媚的笑脸。掬一捧花香在平淡的日子,握着一路相随的暖意,让爱的馨香在柴米油盐中升腾;在一杯茶的温情里,体味生活的诗意;在一碗粥的清淡中,感受生活的浪漫,每天清晨你和阳光都在,便是我的幸福。——春暖花开 《择一城终老,遇一人白首》
  • 相关阅读:
    rsync介绍和配置自动同步
    haproxy 配置httpd和httpds负载均衡
    haproxy配置及使用
    lvs之搭建NAT模式的HTTPS负载集群
    lvs配置之DR模式
    lvs配置之NAT模式
    LVS集群
    lvs四种工作模式,负载调度的介绍
    zabbix页面介绍
    zabbix自定义监控进程、日志、mysql主从
  • 原文地址:https://www.cnblogs.com/-Eric-Liu/p/5563962.html
Copyright © 2011-2022 走看看