В соответствии с этим tutorial у меня был UITextView
внутри UITableViewCell
. Когда загружен UITableView
, я хочу, чтобы UITextView
и UITableViewCell
изменяли размер своей высоты.UITextView внутри UITableViewCell
Но я все сделал на основе учебника, UITextView
не может отобразить весь контент, часть содержимого UITextView
по-прежнему необходима для прокрутки до отображения.
Вот мой код:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *contentTableIdentify = @"contentTableIdentify";
UITableViewCell *cell = nil;
//UILabel *label = nil;
UITextView *textView = nil;
cell = [self.tableView dequeueReusableCellWithIdentifier:contentTableIdentify];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contentTableIdentify];
textView = [[UITextView alloc] initWithFrame:CGRectZero];
[textView setTextColor:[UIColor blackColor]];
[textView setFont:[UIFont fontWithName:@"Helvetica" size:12.0f]];
[textView setBackgroundColor:[UIColor clearColor]];
[textView setTag:1];
[textView setEditable:NO];
[[cell contentView] addSubview:textView];
}
NSString *text = [self.contentArray objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(320 - (10 * 2), 99999.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
if (!textView)
textView = (UITextView *)[cell viewWithTag:1];
[textView setText:text];
[textView setFrame:CGRectMake(10, 10, 320 - (10 * 2), MAX(size.height, 44.0f))];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.contentArray objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
CGFloat height = MAX(size.height, 44.0f);
return height + (10 * 2);
}
Вы делаете эту ячейку в раскадровке, и используете ли вы автоматическую компоновку? – rdelmar