IOS custom cell height

Source: Internet
Author: User

IOS custom cell height

During iOS development, the most important uiviews are UITableView, UIScrollView, and UICollection.


First, create a Model class:


# 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];

}



// If the class does not have an attribute with the same name as the key in the dictionary, this method is executed to overwrite this method to prevent crashes.

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

{




}

@ End


Open cell. h

# Import

@ Class News;


@ Interface NewsListCell: UITableViewCell


@ Property (nonatomic, retain) News * news;



//

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


@ End


Open 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

{

// Title

_ TitleLabel = [[UILabelalloc] initWithFrame: CGRectMake (280, 40)];

_ TitleLabel. backgroundColor = [UIColoryellowColor];

_ TitleLabel. font = [UIFontsystemFontOfSize: 20.0];

[Self. contentViewaddSubview: _ titleLabel];

// Content

_ Summarylabel = [[UILabelalloc] initWithFrame: CGRectMake (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;

// Modify the height of summmartlabel

CGRect summaryrect = _ summarylabel. frame;

Summaryrect. size. height = [[selfclass] summaryheight: news. summary];

_ Summarylabel. frame = summaryrect;


}


// Set the height. Calculate the height of the current row based on the input data.

+ (CGFloat) cellHigth :( News *) news

{

// Variable computing

CGFloat summaryHight = [selfsummaryheight: news. summary];

// The returned result is unchangeable + keb

Return 10 + 40 + 10 + summaryHight + 20;

}


// Calculate the height of news content (either horizontally or vertically fixed)

+ (CGFloat) summaryheight :( NSString *) summary

{

// The size of the moment line required for text rendering. As required, the width is fixed to 280 and the height is set to 10000 (that is, the height is calculated based on the text) the width is related to the label width of the displayed text (the same size)

CGSize contextSize = CGSizeMake (280,100 00 );

// The font size set during calculation, which must be consistent with the font size of the label of the displayed text

NSDictionary * attributes =@{ NSFontAttributeName: [UIFontsystemFontOfSize: 17.0]}; // string provided by the system class. Set the font (17 associated with the label)

CGRect summaryRect = [summaryboundingRectWithSize: contextSize options: NSStringDrawingUsesLineFragmentOriginattributes: attributes context: nil];

Return summaryRect. size. height; // only one of them is required.


}



-(Void) awakeFromNib

{

// Initialization code

}


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

{

[SupersetSelected: selected animated: animated];


// Configure the view for the selected state

}


@ End


Open 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 );

// Register

[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];

}


// Set row high restriction. After cell is set, the cell does not exist when the row is set;

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

{

// Define the method of the class for computing in the cell class. input the data object and return the calculated height.


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


All the data I wrote is detached from a plist file, which is a dictionary type! You can create a trial!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.