Dynamic cell height for iOS development and ios dynamic cell height

Source: Internet
Author: User

Dynamic cell height for iOS development and ios dynamic cell height

During iOS development, we often use UITableView. When talking about UITableView, UITableViewCell is indispensable. so sometimes we have doubts. How can we make the cell height be dynamically designed based on the size of the text and the height of the photo? Next, let's take a look at how we can make the cell highly dynamic and make the interface look more beautiful and harmonious?

// Dynamically set the cell height + (CGFloat) heightForRowWithModel :( PhotoInfo *) photoInfo {// 1. image Height // proportional scaling of images // (1) obtain image UIImage * image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "ZZ" ofType: @ "png"]; CGFloat imageHeight = [self heightForImage: image]; // 2. text height CGFloat textHeight = [self heightForText: photoInfo. introduction]; // 3. return the total height of the cell. return kPhotoCell_TitleLabel_Height + imageHeight + textHeight + 4 * kPhotoCell_MarginBetween;} // calculate the image Height separately + (CGFloat) heightForImage :( UIImage *) image {// (2) obtain the image size CGSize = image. size; // (3) Calculate the zoom ratio CGFloat scale = kPhotoCell_Width/size. width; CGFloat imageHeight = size. height * scale; return imageHeight;} // calculate the height of the text separately + (CGFloat) heightForText :( NSString *) text {// you can specify the font size when calculating the text, calculate the NSDictionary * attrtionary ={ NSFontAttributeName: [UIFont systemFontOfSize: kFontSize]} according to the criteria used. return [text encoding: CGSizeMake (kPhotoCell_Width, 1000) options: Optional | NSStringDrawingUsesFontLeading butes: attr1_context: nil]. size. height ;}

In the code, keys start with macro-defined values.

In ios development, how does one set the height of tableviewcell as the content changes?

In the heightForRowAtIndexPath proxy method

NSString * inforStr = @ ""; UIFont * font = [UIFont systemFontOfSize: 15]; CGSize size = CGSizeMake (); CGSize labelsize = [inforStr sizeWithFont: font limit: size lineBreakMode: NSLineBreakByCharWrapping]; return labelsize. height;
Several Methods for setting the cell dynamic height of UITableView are summarized.

1. Change the loading sequence, or after calculating the cell height, let it load the heightForRowAtIndexPath method again. 2. Directly calculate the heightForRowAtIndexPath and make a judgment, returns the corresponding height. The following is an example of the first method: UITableView (CGFloat) tableView (UITableView // Somewhere in your header: NSIndexPath * selectedCellIndexPath; // And in the implementation file:-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {selectedCellIndexPath = indexPath; // Forces the table view to call heightForRowAtIndexPath [tableView reloadRowsAtIndexPaths: [NSArrayarrayWithObj Ect: indexPath] withRowAnimation: Comment];}-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {// Note: Some operations like calling [tableView cellForRowAtIndexPath: indexPath] // will call heightForRow and thus create a stack overflowif (selectedCellIndexPath! = Nil & amp; [selectedCellIndexPath compare: indexPath] = NSOrderedSame) return 128; return 64;} The reloadRowsAtIndexPaths method will call heightForRowAtIndexPath again to change the cell 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.