An example of an adaptive height for IOS UITableViewCell

Source: Internet
Author: User
Tags uikit

For example, Taobao after the purchase of merchandise evaluation, evaluation of the evaluation list, each individual evaluation of the content of different, evaluation content has less, we generally use UITableView to create the interface, this time need cell adaptive height. code example:

The code is as follows Copy Code

EvaluateTableViewCell.h

#import <UIKit/UIKit.h>

@interface Evaluatetableviewcell:uitableviewcell
@property (Nonatomic,strong) Uilabel *phonelabel;
@property (Nonatomic,strong) Uilabel *timelabel;
@property (Nonatomic,strong) Uilabel *desclabel;
@property (Nonatomic,strong) UIView *intervalview;

Evaluate content and implement automatic line wrapping
-(void) Setintroductiontext: (nsstring*) text;

@end


evaluatetableviewcell.m

-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier
{
self = [super Initwithstyle:style reuseidentifier:reuseidentifier];
if (self) {
User cell phone number
Self.phonelabel = [[Uilabel alloc] Initwithframe:cgrectmake (7, 15, 100, 12)];
Self.phoneLabel.font = Font (12);
[Self AddSubview:self.phoneLabel];
Evaluation time
Self.timelabel = [[Uilabel alloc] Initwithframe:cgrectmake (247 * (kscreenwidth/320), 15, 72, 11)];
Self.timeLabel.font = Font (11);
Self.timeLabel.textColor = [Uicolor Graycolor];
[Self AddSubview:self.timeLabel];
Separator line
UIView *lineview = [[UIView alloc] Initwithframe:cgrectmake (8, Self.timeLabel.maxY +-, kScreenWidth-16, 1)];
Lineview.backgroundcolor = [Uicolor blackcolor];
[Self addsubview:lineview];
Evaluation Content
Self.desclabel = [[Uilabel alloc] Initwithframe:cgrectmake (one, lineview.maxy+, KScreenWidth-22, 13)];
Self.descLabel.font = Font (12);
Self.descLabel.numberOfLines = 0;
[Self AddSubview:self.descLabel];
Interval
Self.intervalview = [[UIView alloc] Initwithframe:cgrectmake (0, Self.descLabel.maxY + 9, Kscreenwidth, 2)];
Self.intervalView.backgroundColor = [Uicolor blackcolor];
[Self addSubview:self.intervalView];
}
return self;
}
Assignment and automatic line wrapping to calculate the cell height
-(void) Setintroductiontext: (nsstring*) text{
Get current Cell height
CGRect frame = [self frame];
Text Assignment
Self.descLabel.text = text;
Set the maximum number of rows for a label
Cgsize size = Cgsizemake (300, 1000);
Cgsize labelsize = [Self.descLabel.text sizeWithFont:self.descLabel.font constrainedtosize:size Linebreakmode: Nslinebreakbyclipping];
Self.descLabel.frame = CGRectMake (self.desclabel.frame.origin.x, SELF.DESCLABEL.FRAME.ORIGIN.Y, Labelsize.width, Labelsize.height);

Calculate the height of the adaptation
Frame.size.height = labelsize.height + 85;
Self.frame = frame;
Self.intervalView.frame = CGRectMake (0, Self.descLabel.maxY + 9, kscreenwidth, 4);
}


Cellzishiyingviewcontroller.m

The code is as follows Copy Code


#import "CellzishiyingViewController.h"
#import "EvaluateTableViewCell.h"

@interface Cellzishiyingviewcontroller () <uitableviewdatasource,uitableviewdelegate>{
UITableView *evaluatetableview;
Uilabel *evaluatelabel;
Uilabel *goodevaluatelabel;
}

@end

@implementation Cellzishiyingviewcontroller


-(void) Viewdidload {
[Super Viewdidload];
Self.view.backgroundColor = RGB (242, 242, 247);
Self.automaticallyadjustsscrollviewinsets = NO;
Evaluatetableview = [[UITableView alloc] Initwithframe:cgrectmake (0, Kheaderheight, Kscreenwidth, KScreenHeight- kheaderheight) Style:uitableviewstyleplain];
Evaluatetableview.delegate = self;
Evaluatetableview.datasource = self;
Evaluatetableview.separatorstyle = Uitableviewcellseparatorstylenone;
[Self.view Addsubview:evaluatetableview];
}
#pragma mark-Okay.
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
return 4;
}
#pragma mark-row height
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{
return 95;
Evaluatetableviewcell *cell = [self Tableview:evaluatetableview cellforrowatindexpath:indexpath];
return cell.frame.size.height;
}
#pragma mark-each line of content
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
static NSString *indefier = @ "Cell";
Evaluatetableviewcell *cell = [TableView dequeuereusablecellwithidentifier:indefier];
if (!cell) {
cell = [[Evaluatetableviewcell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:indefier];
}
Nsarray *onearray = @[@ "18374637823", @ "18643535325", @ "15653543746", @ "13924366238"];
Nsarray *twoarray = @[@ "2016-04-04", @ "2016-04-06", @ "2016-05-04", @ "2016-09-04"];
Nsarray *threearray = @[@ "Because iOS is designed to follow the MVC pattern, many operations are communicated through the agent and the outside world, but for data source controls, there is a data source attribute in addition to the proxy, which interacts with the outside world." ", @" UITableView has two styles: Uitableviewstyleplain and uitableviewstylegrouped. There is no essential difference between the two, but the latter is displayed in the group style as shown by the Normal style ", @" because iOS is designed to follow the MVC pattern, many operations are communicated through the agent and the outside, but for the data source control there is a data source property besides the agent, It interacts with the outside world through data. The Uitableviewdatasource protocol is required to implement the DataSource after the UITableView is set up, and a variety of data manipulation methods are defined in this Protocol ", @" to effectively carry out criticism and self-criticism, to expose and correct work "];
Cell.selectionstyle = Uitableviewcellselectionstylenone;
Cell.phoneLabel.text = Onearray[indexpath.row];
Cell.timeLabel.text = Twoarray[indexpath.row];
Cell.descLabel.text = Threearray[indexpath.row];
[Cell SetIntroductionText:cell.descLabel.text];
return cell;
}

Note:

  code is as follows copy code


//Current screen width
#define kscreenwidth    [UIScreen mainscreen].bounds.size.width
//Current screen height
#define kscreenheight   [UIScreen mainscreen].bounds.size.height

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.