2016-02-10 2 views
2

Я создал пользовательскую ячейку с текстовым просмотром. Мне нужно увеличить высоту текстового поля и высоту настраиваемой ячейки в соответствии с содержимым текстового поля без использования автоматической компоновки. Может ли один помочь мнеКак изменить пользовательскую высоту ячейки таблицы и высоту текста в соответствии с ее содержимым в текстовом виде

Cell design Это код:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return 80; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
return sendername.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString *CellIdentifier1 = @"MenuCtrl"; 

tsecureMsgConCell *cell = [tabVieConv dequeueReusableCellWithIdentifier:CellIdentifier1]; 

if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MsgConCell" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 

cell.userName.text  = [sendername objectAtIndex:indexPath.row]; 
cell.msgContentTxView.text  = [msgContents objectAtIndex:indexPath.row]; 


NSString * sentDateCell = [msgSenddate objectAtIndex:indexPath.row]; 
NSString * replyDateCell = [msgRevidate objectAtIndex:indexPath.row]; 
thredIdCell    = [TSecureMsgId objectAtIndex:indexPath.row]; 

if ([sentDateCell isEqualToString:@"NULL"]) 
{ 
    cell.dateLbl.text = [msgRevidate objectAtIndex:indexPath.row]; 
    cell.timeLbl.text = [msgRecTime objectAtIndex:indexPath.row]; 
    cell.imgVew.image = [UIImage imageNamed:@"recive.png"]; 
} 

else if ([replyDateCell isEqualToString:@"NULL"]) 
{ 
    cell.dateLbl.text = [msgSenddate objectAtIndex:indexPath.row]; 
    cell.timeLbl.text = [msgSendTime objectAtIndex:indexPath.row]; 
    cell.imgVew.image = [UIImage imageNamed:@"sent.png"]; 
} 

return cell; 

} 

msgContentTxView - мой TextView

+0

Возможный дубликат (HTTP: [UITableViewCell с высотой UITextView в прошивке 7?] //stackoverflow.com/questions/18368567/uitableviewcell-with-uitextview-height-in-ios-7) –

+0

Сначала попробуйте найти ответ перед отправкой вопроса. –

ответ

0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger row = indexPath.row; 
    if (row != NSNotFound) 
    { 


     if ([exapmlestring length] > 0) 
     { 
      CGFloat padding = 17.0f; 

      CGFloat labelWidth = self.tableview.bounds.size.width - padding*2; 
      NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys: 
                [UIFont fontWithName:@"HelveticaNeue" size:17.0], NSFontAttributeName, 
                nil]; 

      NSAttributedString *text = [[NSAttributedString alloc] initWithString:exapmlestring attributes:attributesDictionary]; 
      NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin; 

///size 
      CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX) 
                options:options 
                context:nil]; 


      boundingRect.size.height = boundingRect.size.height + 100; 

      return (CGFloat) (ceil(boundingRect.size.height) + padding*2); 
     } 
    } 

    return 0; 
} 
+0

boundingRect используется для получения высоты для текстового поля – Abhimanyu

Смежные вопросы