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.