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