IOS dynamically changes cell height

Source: Internet
Author: User

# Import "ViewController. h"

@ Interface ViewController ()
{
UITableView * _ table;
NSMutableArray * _ dataList;
UIView * footView;
}
@ End

@ Implementation ViewController

-(Void) viewDidLoad
{
[Super viewDidLoad];
_ Table = [[UITableView alloc] initWithFrame: CGRectMake (0, 0, self. view. frame. size. width, self. view. frame. size. height)];
_ Table. delegate = self;
_ Table. dataSource = self;
// _ Table. multipleTouchEnabled = YES;
[Self. view addSubview: _ table];
_ DataList = [[NSMutableArray alloc] initWithObjects: @ "1111n111n111n111n", @ "222", @ "333", @ "444", @ "555", @ "666 ", @ "777", nil];
FootView = [[UIView alloc] initWithFrame: CGRectMake (0, 0,320, 80)];
FootView. backgroundColor = [UIColor yellowColor];
_ Table. tableFooterView = footView;
}

-(Void) didReceiveMemoryWarning
{
[Super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


# Pragma mark-UITableView
-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
{
Return 1;
}

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
{
Return [_ dataList count];
}


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

Static NSString * CellIdentifier = @ "Cell ";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil ){
Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];
UILabel * label = [[UILabel alloc] initWithFrame: CGRectZero];
Label. tag = 1;
// Label. lineBreakMode = UILineBreakModeCharacterWrap;
Label. lineBreakMode = NSLineBreakByCharWrapping;
Label. highlightedTextColor = [UIColor whiteColor];
Label. numberOfLines = 0;
Label. opaque = NO; // If Opaque is selected, NO content behind the view should be drawn.
Label. backgroundColor = [UIColor clearColor];
[Cell. contentView addSubview: label];

}

UILabel * label = (UILabel *) [cell viewWithTag: 1];
NSString * text;
Text = [_ dataList objectAtIndex: indexPath. row];
CGRect cellFrame = [cell frame];
CellFrame. origin = CGPointMake (0, 0 );

Label. text = text;
CGRect rect = CGRectInset (cellFrame, 2, 2 );
Label. frame = rect;
[Label sizeToFit];
If (label. frame. size. height> 46 ){
CellFrame. size. height = 50 + label. frame. size. height-46;
}
Else {
CellFrame. size. height = 50;
}
[Cell setFrame: cellFrame];

Return cell;
}

-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath
{
UITableViewCell * cell = [self tableView: tableView cellForRowAtIndexPath: indexPath];
Return cell. frame. size. height;
}

 

@ End

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.