码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 自定义cell的高度

时间:2015-03-09 14:27:55      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:delegate   uitableview   

在iOS开发过程中,最重要的几个UIView分别为UITableView、UIScrollView、UICollection.今天由小白哥带大家认识一下UItableVIew


首先,建立一个Model类:


#import <Foundation/Foundation.h>


@interface News : NSObject


@property (nonatomic,retain) NSString *title;

@property (nonatomic,retain) NSString *summary;



@end


News.m

#import "News.h"


@implementation News


- (void)dealloc

{

    self.summary = nil;

    self.title = nil;

    [super dealloc];

}



//类中不存在与字典中key同名的属性时,会执行这个方法,重写该方法防治崩溃

- (void)setValue:(id)value forUndefinedKey:(NSString *)key

{


  



}

@end


打开cell.h

#import <UIKit/UIKit.h>

@class News;


@interface NewsListCell : UITableViewCell


@property (nonatomic,retain) News *news;



//

+ (CGFloat)cellHigth:(News *)news;


@end


打开cell.m

#import "NewsListCell.h"

#import "News.h"


@interface NewsListCell ()

{


    UILabel *_titleLabel;

    UILabel *_summarylabel;





}

@end



@implementation NewsListCell

- (void)dealloc

{    self.news = nil;

    [_summarylabel release];

    [_titleLabel release];

    [super dealloc];

}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // Initialization code

        [self setuoSubviews];

    }

    return self;

}


- (void)setuoSubviews

{

    

    //标题

    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 40)];

    _titleLabel.backgroundColor = [UIColor yellowColor];

    _titleLabel.font = [UIFont systemFontOfSize:20.0];

    [self.contentView addSubview:_titleLabel];

    

    //内容

    _summarylabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 280, 30)];

    _summarylabel.backgroundColor = [UIColor cyanColor];

    _summarylabel.font = [UIFont systemFontOfSize:17.0];

    _summarylabel.numberOfLines = 0;

    [self.contentView addSubview:_summarylabel];

    

    

}





- (void)setNews:(News *)news

{

    if (_news != news) {

        [_news release];

        _news = [news retain];

    }

    _titleLabel.text = news.title;

    _summarylabel.text = news.summary;

    

    //修改summmartlabel的高度

    CGRect summaryrect = _summarylabel.frame;

    summaryrect.size.height = [[self class] summaryheight:news.summary];

    _summarylabel.frame = summaryrect;

    


}


//设置高度,根据传入数据,计算当前行高

+ (CGFloat)cellHigth:(News *)news

{

    

    //计算可变

    CGFloat summaryHight = [self summaryheight:news.summary];

    

    

    //返回不可变+keb

    

    return 10 + 40 + 10 + summaryHight + 20;

    

    

    

}


//计算新闻内容 的高度(横向或竖向固定)

+ (CGFloat)summaryheight:(NSString *)summary

{

    //文本渲染时需要的矩行大小,按需求:宽度固定位280,高度设置为10000(即高度根据文本计算得到的) 宽度与显示文本的label的宽度有关(一样大)

    CGSize contextSize = CGSizeMake(280, 10000);

    

    //计算时设置的字体大小,必须与显示文本的label的字体大小保持一致

    NSDictionary *attributes  = @{NSFontAttributeName:[UIFont systemFontOfSize:17.0]};//系统类提供的字符串.设置字体 (label有关 17)

   

      CGRect summaryRect = [summary boundingRectWithSize:contextSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];

    

    return summaryRect.size.height;//只需要其中一个


    

    

}



- (void)awakeFromNib

{

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated

{

    [super setSelected:selected animated:animated];


    // Configure the view for the selected state

}


@end


打开UITableViewController.m为:

//

//  NewsListTableViewController.m

//  Lesson11Cellhight

//

//  Created by Dubai on 14-9-30.

//  Copyright (c) 2014 Dubai All rights reserved.

//


#import "NewsListTableViewController.h"


#import "NewsListCell.h"


#import "News.h"


#define kNewsCell @"NewsListCell"

@interface NewsListTableViewController ()

{


    NSMutableArray *_newsArray;




}

@end


@implementation NewsListTableViewController

- (void)dealloc

{

    [_newsArray release];

    [super dealloc];

}

- (id)initWithStyle:(UITableViewStyle)style

{

    self = [super initWithStyle:style];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

    

    

    

    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"NewsData" ofType:@"plist"];

    NSDictionary *sourceDic = [NSDictionary dictionaryWithContentsOfFile:filepath];

    _newsArray = [[NSMutableArray alloc] initWithCapacity:50];

    

    NSArray *sourceArray = sourceDic[@"news"];

   // NSLog(@"source array = %@",sourceArray);

    

    //NSMutableArray *newsArray = [[NSMutableArray alloc] initWithCapacity:50];

    for (NSDictionary *newsDic in sourceArray) {

        News *news = [[News alloc] init];

        [news setValuesForKeysWithDictionary:newsDic];

        [_newsArray addObject:news];

        [news release];

    }

    NSLog(@"_newsArray = %@",_newsArray);

    

    //注册

    [self.tableView registerClass:[NewsListCell class] forCellReuseIdentifier:kNewsCell];

    


    

    

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

    if ([self isViewLoaded]  && self.view.window ==nil ) {

        self.view = nil;

        

    }

    

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{


    // Return the number of sections.

    //return [_newsArray count];

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{


    // Return the number of rows in the section.

    return [_newsArray count];

}


//设置行高限制性,设置cell后执行,即执行设置行高时 cell不存在对象;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //cell类中 计算,定义类的方法.传入数据对象,返回计算后的高度


    News *news = _newsArray[indexPath.row];

    return [NewsListCell cellHigth:news];

    

    //return 110;



}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    //News *bnews = [[News alloc] init];

    NewsListCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsCell forIndexPath:indexPath];

    

    News *anews = _newsArray[indexPath.row];

//    cell.new = news.title;

//    cell.new = news.summary;

    //cell.news =bnews;

    cell.news = anews;

        return cell;

}


@end


我写的这个 所有的数据卸载一个plist文件里是一个字典类型!大家可以创建一个试一下!



iOS 自定义cell的高度

标签:delegate   uitableview   

原文地址:http://blog.csdn.net/zhaoguodongios/article/details/44154049

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