iOS 自訂cell的高度
在iOS開發過程中,最重要的幾個UIView分別為UITableView、UIScrollView、UICollection.今天由小白哥帶大家認識一下UItableVIew
首先,建立一個Model類:
#import
@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;
[superdealloc];
}
//類中不存在與字典中key同名的屬性時,會執行這個方法,重寫該方法防治崩潰
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
開啟cell.h
#import
@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;
[_summarylabelrelease];
[_titleLabel release];
[superdealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[selfsetuoSubviews];
}
return self;
}
- (void)setuoSubviews
{
//標題
_titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(20,10, 280, 40)];
_titleLabel.backgroundColor = [UIColoryellowColor];
_titleLabel.font = [UIFontsystemFontOfSize:20.0];
[self.contentViewaddSubview:_titleLabel];
//內容
_summarylabel = [[UILabelalloc] initWithFrame:CGRectMake(20,60, 280, 30)];
_summarylabel.backgroundColor = [UIColorcyanColor];
_summarylabel.font = [UIFontsystemFontOfSize:17.0];
_summarylabel.numberOfLines =0;
[self.contentViewaddSubview:_summarylabel];
}
- (void)setNews:(News *)news
{
if (_news != news) {
[_newsrelease];
_news = [news retain];
}
_titleLabel.text = news.title;
_summarylabel.text = news.summary;
//修改summmartlabel的高度
CGRect summaryrect = _summarylabel.frame;
summaryrect.size.height = [[selfclass] summaryheight:news.summary];
_summarylabel.frame = summaryrect;
}
//設定高度,根據傳入資料,計算當前行高
+ (CGFloat)cellHigth:(News *)news
{
//計算可變
CGFloat summaryHight = [selfsummaryheight: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:[UIFontsystemFontOfSize:17.0]};//系統類別提供的字串.設定字型 (跟label有關 17)
CGRect summaryRect = [summaryboundingRectWithSize:contextSize options:NSStringDrawingUsesLineFragmentOriginattributes:attributes context:nil];
return summaryRect.size.height;//只需要其中一個
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[supersetSelected: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];
[superdealloc];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [superinitWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
NSString *filepath = [[NSBundlemainBundle] pathForResource:@"NewsData"ofType:@"plist"];
NSDictionary *sourceDic = [NSDictionarydictionaryWithContentsOfFile:filepath];
_newsArray = [[NSMutableArrayalloc] initWithCapacity:50];
NSArray *sourceArray = sourceDic[@"news"];
// NSLog(@"source array = %@",sourceArray);
//NSMutableArray *newsArray = [[NSMutableArray alloc] initWithCapacity:50];
for (NSDictionary *newsDicin sourceArray) {
News *news = [[Newsalloc] init];
[news setValuesForKeysWithDictionary:newsDic];
[_newsArrayaddObject:news];
[newsrelease];
}
NSLog(@"_newsArray = %@",_newsArray);
//註冊
[self.tableViewregisterClass:[NewsListCellclass] forCellReuseIdentifier:kNewsCell];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// 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 [_newsArraycount];
}
//設定行高限制性,設定cell後執行,即執行設定行高時 cell不存在對象;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//在cell類中計算,定義類的方法.傳入資料對象,返回計算後的高度
News *news = _newsArray[indexPath.row];
return [NewsListCellcellHigth:news];
//return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//News *bnews = [[News alloc] init];
NewsListCell *cell = [tableViewdequeueReusableCellWithIdentifier:kNewsCellforIndexPath:indexPath];
News *anews = _newsArray[indexPath.row];
// cell.new = news.title;
// cell.new = news.summary;
//cell.news =bnews;
cell.news = anews;
return cell;
}
@end
我寫的這個 所有的資料卸載一個plist檔案裡是一個字典類型!大家可以建立一個試一下!