标签:
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 }
UITableViewCell的 Cell 内的图片自适应高度
标签:
原文地址:http://www.cnblogs.com/-Eric-Liu/p/5563962.html