你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UITableCell自適應高度

iOS UITableCell自適應高度

編輯:IOS開發綜合

列如--展示新聞信息列表.

 

首先得有一個Model類--New

 

New.h為:

 

//

// News.h

// Cellhight

//

// Created by Dubai on 15-5-7.

// Copyright (c) 2015年 Dubai. All rights reserved.

//

 

#import

 

@interface News : NSObject

 

@property (nonatomic,retain) NSString *title;

@property (nonatomic,retain) NSString *summary;

 

 

@end


New.m為:

 

 

//

// News.h

// Cellhight

//

// Created by Dubai on 15-5-7.

// Copyright (c) 2015年 Dubai. All rights reserved.

//

 

#import "News.h"

 

@implementation News

 

- (void)dealloc

{

self.summary = nil;

self.title = nil;

[super dealloc];

}

 

 

//類中不存在與字典中key同名的屬性時,會執行這個方法,重寫該方法防治崩潰

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

{

 

 

 

 

}

@end


其次創建一個UITableCell類來進行 設置cell的高度

NewsListCell.h的代碼為:

 

//

// NewsListCell.h

// Cellhight

//

// Created by DUbai on 15-5-7.

// Copyright (c) 2015年 Dubai. All rights reserved.

//

 

#import

@class News;

 

@interface NewsListCell : UITableViewCell

 

@property (nonatomic,retain) News *news;

 

 

//

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

 

@end


NewsListCell.m的代碼為:

 

 

//

// NewsListCell.m

// Lesson11Cellhight

//

// Created by DUbai on 15-5-7.

// Copyright (c) 2015年 Dubai. All rights reserved.

//

 

#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


控制器裡的代碼為:

 

 

//

// NewsListTableViewController.m

// Lesson11Cellhight

//

// Created by Dubai on 15-5-7.

// Copyright (c) 2015年 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;

}

 

/*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

// Return NO if you do not want the specified item to be editable.

return YES;

}

*/

 

/*

// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete the row from the data source

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

} else if (editingStyle == UITableViewCellEditingStyleInsert) {

// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

}

}

*/

 

/*

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath

{

}

*/

 

/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

// Return NO if you do not want the item to be re-orderable.

return YES;

}

*/

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

 

@end



 

 

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved