如果您使用的是原型单元格,那么每次只要标识符正确,dequeueReusableCellWithIdentifier就会返回一个单元格。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ScribbleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
Scribble *scribble = scribble[indexPath.row];
UIImageView *scribbleImageView = (UIImageView *)[cell viewWithTag:100];
scribbleImageView.image = [UIImage imageNamed:scribble.image];
scribbleImageView.layer.cornerRadius = 18.0;
scribbleImageView.clipsToBounds = YES;
UILabel *scribbleNameLabel = (UILabel *)[cell viewWithTag:101];
scribbleNameLabel.text = scribble.title;
UILabel *scribbleBodyLabel = (UILabel *)[cell viewWithTag:102];
[scribbleBodyLabel setLineBreakMode:NSLineBreakByWordWrapping];
scribbleBodyLabel.numberOfLines = 0;
scribbleBodyLabel.text = scribble.body;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Scribble *scribble = scribble[indexPath.row];
NSString *scribbleBody = scribble.body;
UIFont *cellFont = [UIFont systemFontOfSize:17.0f];
/*Instead of hardcoded width you can find width from tableView frame
imageView and their offsets 20 for tableView ,40 for imageView, 30 for
offset of body label */
CGFloat width = tableView.frame.size.width - 90.0f;
CGSize constraintSize = CGSizeMake(width, MAXFLOAT);
CGSize labelSize = [scribbleBody sizeWithFont:cellFont
constrainedToSize:constraintSize
lineBreakMode:NSLineBreakByWordWrapping];
//Add the height of scribbleNameLabel +offset
return labelSize.height + 40.0f;
}