Ios-ui control UITableView (ii)-custom cell with unequal height

Source: Internet
Author: User

Cell with unequal height adds frame data to the model
    • Frame for all child controls
    • The height of the cell
@interface XMGStatus : NSObject/**** 文字\图片数据 ****/// ...../**** frame数据 ****//** 头像的frame */@property (nonatomic, assign) CGRect iconFrame;// ...../** cell的高度 */@property (nonatomic, assign) CGFloat cellHeight;@end
    • Override Model Cellheight property's Get method
- (CGFloat)cellHeight{    if (_cellHeight == 0) {        // ... 计算所有子控件的frame、cell的高度    }    return _cellHeight;}
In the controller
    • Implement a proxy method that returns the cell height
      • Return the Indexpath position to the cell's height in this method
/** *  返回每一行cell的具体高度 */- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    XMGStatus *status = self.statuses[indexPath.row]; return status.cellHeight;}
    • Passing model data to the cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *ID = @"tg"; // 访问缓存池 XMGStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 设置数据(传递模型数据) cell.status = self.statuses[indexPath.row]; return cell;}
Create a new inherit fromUITableViewCellSubclasses such as Xmgstatuscell.
@interface XMGStatusCell : UITableViewCell@end
In the xmgstatuscell.m file
    • overriding -initWithStyle:reuseIdentifier: method
      • In this method, add all the child controls that need to be displayed
      • Make some initialization settings (set font, text color, etc.) for child controls
/** *  在这个方法中添加所有的子控件 */- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { // ...... } return self;}
Provide a model attribute in the XMGStatusCell.h file, such as the XMGTG model
@class XMGStatus;@interface XMGStatusCell : UITableViewCell/** 团购模型数据 */@property (nonatomic, strong) XMGStatus *status;@end
Set method for overriding model properties in XMGTGCELL.M
    • To set model data for a child control in a set method
- (void)setStatus:(XMGStatus *)status{    _status = status;    // .......}
Rewrite-layoutSubviewsMethod
    • Be sure to call[super layoutSubviews]
    • Set the frame of all child controls in this method
/** *  在这个方法中设置所有子控件的frame */- (void)layoutSubviews{    [super layoutSubviews];    // ......}

Ios-ui control UITableView (ii)-custom cell with unequal height

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.