码迷,mamicode.com
首页 > 其他好文 > 详细

UITableView(五)

时间:2016-03-02 23:39:37      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

 1 #import "ViewController.h"
 2 #import "FirstModel.h"
 3 #import "FirstTableViewCell.h"
 4 #import "Tool.h"
 5 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
 6 @property(nonatomic,strong) NSMutableArray *saveData;
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     // Do any additional setup after loading the view, typically from a nib.
14     //创建UITableView
15     [self createTableView];
16     self.saveData = [[NSMutableArray alloc] init];
17     //创建数据
18     [self createData];    
19    
20 }
21 //创建数据
22 -(void)createData
23 {
24     //使用KVC给model对象进行赋值
25     NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"1.jpg",@"imageName",@"我们有什么理由不为他们点赞呢?  我们注意到,一些创业者或者投资人,似乎并不喜欢“连续创业者”这样的称谓,仅仅因为他们觉得,这似乎意味着失败,代表了“你不行”。他们更看重的是那种连续的“成功创业者”。的确,成功本身就有说服力——不过这也并不意味着,一个成功过的创业者可以更容易地复制成功。当然,我们的焦距不会局限于曾经历失败的创业者,我们更在意的是这种持续的冒险精神和创新能力,无论是连续的成功或者曾经历失败之后的坚持,他们改变着自己,也改变着他们和我们共同生活的这个世界。如果说这个世界正变得更多元化,也更具想象力,很大程度上,这是商业的力量所致",@"text", nil];
26     FirstModel *firstModel = [[FirstModel alloc] init];
27     [firstModel setValuesForKeysWithDictionary:dic1];
28         [self.saveData addObject:firstModel];
29     
30 }
31 //创建UITableView
32 -(void)createTableView
33 {
34     CGRect rect = [UIScreen mainScreen].bounds;
35     UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, rect.size.width, rect.size.height - 20) style:UITableViewStyleGrouped];
36     tableView.dataSource = self;
37     tableView.delegate = self;
38     [self.view addSubview:tableView];
39     NSLog(@"创建UITableView");
40     
41 }
42 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
43 {
44     id object = [self.saveData objectAtIndex:indexPath.row];
45     FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
46     if (cell == nil) {
47         cell = [[FirstTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
48     }
49     cell.model = object;
50     NSLog(@"添加cell");
51     return cell;
52 }
53 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
54 {
55     CGFloat iamgeHeight = [Tool imageScaleHeightWith:@"1.jpg"];
56     NSString *text = @"我们有什么理由不为他们点赞呢?  我们注意到,一些创业者或者投资人,似乎并不喜欢“连续创业者”这样的称谓,仅仅因为他们觉得,这似乎意味着失败,代表了“你不行”。他们更看重的是那种连续的“成功创业者”。的确,成功本身就有说服力——不过这也并不意味着,一个成功过的创业者可以更容易地复制成功。当然,我们的焦距不会局限于曾经历失败的创业者,我们更在意的是这种持续的冒险精神和创新能力,无论是连续的成功或者曾经历失败之后的坚持,他们改变着自己,也改变着他们和我们共同生活的这个世界。如果说这个世界正变得更多元化,也更具想象力,很大程度上,这是商业的力量所致";
57     CGFloat textHeight = [Tool lableHeightWithText:text font:[UIFont systemFontOfSize:12]];
58     NSLog(@"返回高度");
59     return iamgeHeight + textHeight;
60 
61 }
62 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
63 {
64     return self.saveData.count;
65 }
66 - (void)didReceiveMemoryWarning {
67     [super didReceiveMemoryWarning];
68     // Dispose of any resources that can be recreated.
69 }
70 
71 @end
1 #import <UIKit/UIKit.h>
2 @class FirstModel;
3 @interface FirstTableViewCell : UITableViewCell
4 @property (nonatomic,strong) UILabel *showTextLable;
5 @property (nonatomic,strong) UIImageView *showImageView;
6 @property (nonatomic,strong) FirstModel *model;
7 @end
 1 #import "FirstTableViewCell.h"
 2 #import "FirstModel.h"
 3 #import "Tool.h"
 4 @implementation FirstTableViewCell
 5 
 6 - (void)awakeFromNib {
 7     // Initialization code
 8 }
 9 -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
10 {
11     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
12     if (self) {
13         self.showTextLable = [[UILabel alloc] initWithFrame:CGRectZero];
14         self.showImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
15         [self.contentView addSubview:self.showImageView];
16         [self.contentView addSubview:self.showTextLable];
17     }
18     return self;
19 }
20 -(void)layoutSubviews
21 {
22     //设置空间的frame 和数据
23     CGFloat lableHeight = [Tool lableHeightWithText:self.model.text font:self.showTextLable.font];
24     self.showTextLable.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, lableHeight);
25     self.showTextLable.numberOfLines = 0;
26     self.showTextLable.text = self.model.text;
27     
28     //设置imageView
29     CGFloat imageHeight = [Tool imageScaleHeightWith:self.model.imageName];
30     //self.imageView.frame = CGRectMake(0, lableHeight, [UIScreen mainScreen].bounds.size.width, imageHeight);
31     self.showImageView.frame = CGRectMake(0, lableHeight, [UIScreen mainScreen].bounds.size.width, imageHeight);
32     
33     self.imageView.backgroundColor = [UIColor greenColor];
34     self.showImageView.image = [UIImage imageNamed:self.model.imageName];
35     NSLog(@"成功添加图片");
36 }
37 
38 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
39     [super setSelected:selected animated:animated];
40 
41     // Configure the view for the selected state
42 }
43 
44 @end
1 #import <Foundation/Foundation.h>
2 
3 @interface FirstModel : NSObject
4 @property (nonatomic,strong) NSString *text;
5 //图片的名字
6 @property (nonatomic,strong) NSString *imageName;
7 @end
1 #import "FirstModel.h"
2 
3 @implementation FirstModel
4 -(void)setValue:(id)value forUndefinedKey:(NSString *)key
5 {
6     
7 }
8 @end
1 #import <Foundation/Foundation.h>
2 #import <UIKit/UIKit.h>
3 @interface Tool : NSObject
4 //创建类方法计算lable的高度
5 +(CGFloat)lableHeightWithText:(NSString *)text font:(UIFont *)font;
6 //创建类方法计算imageView的高度
7 +(CGFloat)imageScaleHeightWith:(NSString *)imageName;
8 @end
 1 #import "Tool.h"
 2 
 3 @implementation Tool
 4 //创建类方法计算lable的高度
 5 +(CGFloat)lableHeightWithText:(NSString *)text font:(UIFont *)font
 6 {
 7     NSDictionary *dic = @{NSFontAttributeName:font};
 8     CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, 100000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
 9     return rect.size.height;
10 }
11 //创建类方法计算imageView的高度
12 
13 +(CGFloat)imageScaleHeightWith:(NSString *)imageName
14 {
15     UIImage *image = [UIImage imageNamed:imageName];
16     CGFloat width = image.size.width;
17     CGFloat height = image.size.height;
18     return height / width * [UIScreen mainScreen].bounds.size.width;
19 }
20 @end

 

UITableView(五)

标签:

原文地址:http://www.cnblogs.com/DevinSMR/p/5236709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!