IOS learning, ios Learning
1. Custom Cell
@ Property (weak, nonatomic) IBOutlet UILabel * testLabel; + (instancetype) initTableViewCell :( UITableView *) tableView; // Cell height method-(void) cellAutoLayoutHeight :( NSString *) str;
Cell. m
-(void)cellAutoLayoutHeight:(NSString *)str{ self.testLabel.numberOfLines = 0; self.testLabel.lineBreakMode = NSLineBreakByWordWrapping; self.testLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.testLabel.frame); self.testLabel.text = str;}+(instancetype)initTableViewCell:(UITableView *)tableView{ TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell"]; if (cell == nil) { cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TableViewCell"]; } return cell;}
2. cellForRowAtIndexPath
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ TableViewCell *cell = [TableViewCell initTableViewCell:tableView]; cell.testLabel.text = _str; cell.testLabel.numberOfLines = 0; return cell;}
HeightForRowAtIndexPath
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ TableViewCell *cell = [TableViewCell initTableViewCell:tableView]; [cell cellAutoLayoutHeight:_str]; CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize]; return size.height + 1;}
As shown in the right