eightForRowAtIndexPath那个实现方法确实不太好,会dequeue掉一个可以reuse的cell导致经常都要新创建cell
- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
NSString *content = [examData objectAtIndex:indexPath.row];
UIFont *font = [UIFont systemFontOfSize:17.0];
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(self.view.bounds.size.width -40 , MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"heightForRowAtIndexPath");
return size.height + 16;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellExam";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
// cell.textLabel.text = [examData objectAtIndex:indexPath.row];
// cell.textLabel.numberOfLines = 0;
// cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
// cell.textLabel.font = [UIFont systemFontOfSize:16];
UILabel *cellLabel = (UILabel *) [cell viewWithTag:2] ;
//設置自動行數與字符換行
cellLabel.lineBreakMode = UILineBreakModeWordWrap;
cellLabel.numberOfLines = 0;
[cellLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
NSLog(@"cellForRowAtIndexPath");
//[cellLabel setFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
NSString *text = [examData objectAtIndex:indexPath.row];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[cellLabel setText:text];
[cellLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
iPhone实战:动态改变UITableView中的Cell高度
UILabel显示换行的方法
//fix label height
labelPinyinQ.lineBreakMode = UILineBreakModeWordWrap;
labelPinyinQ.numberOfLines=0;
CGSize size = CGSizeMake(200,100);
UIFont *font = [UIFont fontWithName:@"Heiti SC" size:FONT_SIZE_PINYIN];
CGSize labelsize = [trimsPinyinText sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap];
[labelPinyinQ setFrame:CGRectMake(0,0, 200, labelsize.height)];
[viewQA addSubview:labelPinyinQ];
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。