2011-10-05 6 views
0

동적 인 TextView와 수직으로 가운데 정렬해야하는 다른 객체를 가진 UITableViewCell이 있습니다.UITableview, 동적 높이가있는 셀의 개체를 세로로 가운데에 맞 춥니 다?

지금 나는 UITextView의 높이를 계산하고 모든 것을 제자리에 설정하기 위해 참조로 사용합니다.

내가 가진 문제는 때로는 잘못 계산되고 항상 중간에 있어야하는 "priceLabel"이 아래쪽에 있다는 것입니다. 세포가 다시 눈에 띄게 될 수 있도록 스크롤하는 시간의 20 %가 발생합니다.

세포의 높이를 얻는 방법이 있습니까? 왜 잘못 계산합니까? 또한이 문제가 나타날 때 UITextView를 절반으로 줄입니까?

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    CGSize size; 
    NSString *text; 
    /* Height of each cell */ 
    text = @"Some different lengths of text"; 
    size = [text sizeWithFont:[UIFont fontWithName:@"TrebuchetMS" size:17] constrainedToSize:CGSizeMake(theTableView.bounds.size.width - 260, 800) lineBreakMode:UILineBreakModeWordWrap]; 

    float totalSize = size.height + 10; 
    if (totalSize < 120) { 
      return 120; 
    } else { 
     return totalSize; 
    } 
} 


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

    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     CGSize size; 
     NSString *text; 
     float totalSize; 
     /* Height of each cell */ 
     text = @"Some different lengths of text"; 
     size = [text sizeWithFont:[UIFont fontWithName:@"TrebuchetMS" size:17] constrainedToSize:CGSizeMake(theTableView.bounds.size.width - 260, 800) lineBreakMode:UILineBreakModeWordWrap]; 
      if (size.height + 10 < 120) { 
       totalSize = 120; 
      } else { 
       totalSize = size.height + 10; 
      } 
     } 

     /* Createing a new Cell */ 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

     /* Create the text Label */ 
     textView = [[[UITextView alloc] initWithFrame: CGRectMake(90,35,theTableView.bounds.size.width - 260 , totalSize)] autorelease]; 
     [cell.contentView addSubview:textView]; 
     textView.tag = TEXT_LABEL_TAG; 
     textView.backgroundColor = [UIColor clearColor]; 
     textView.textColor = [UIColor blackColor]; 
     textView.font = [UIFont fontWithName:@"TrebuchetMS" size:17]; 
     textView.editable = NO; 
     textView.userInteractionEnabled = NO; 

     /* Create price Label */ 
     priceLabel = [[[UILabel alloc] initWithFrame: CGRectMake(theTableView.bounds.size.width - 225, (totalSize/2) - (35/2), 120 , 35)] autorelease]; 
     [cell.contentView addSubview:priceLabel]; 
     priceLabel.tag = PRICE_LABEL_TAG; 
     priceLabel.textAlignment = UITextAlignmentRight; 
     priceLabel.backgroundColor = [UIColor clearColor]; 
     priceLabel.textColor = [UIColor redColor]; 
     priceLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:22]; 

답변

0

좌표가 정수 수학 인 경우를 제외하면 코드는 괜찮아 보입니다. 그래서 사용해보십시오

priceLabel = [[[UILabel alloc] initWithFrame: CGRectMake(theTableView.bounds.size.width - 225, (totalSize/2.f) - (35.f/2.f), 120 , 35)] autorelease]; 

도움이 될 수는 있지만 확실하지 않습니다.

1

initWithFrame :에서 호출하는 것처럼 하위 뷰의 프레임 프레임과 해당 위치를 동시에 정의했을 때 몇 가지 결함이있었습니다. 프레임이 변환 중에 매우 강건하지 않은 것 같습니다. 하위보기를 초기화 한 후에 경계 및 중심 속성을 설정하는 것이 더 효과적이었습니다. 이렇게하면 센터링을보다 잘 제어 할 수 있습니다.

label.bounds = CGRectMake(0,0,0.6*self.view.frame.size.width,30); 
label.center = CGPointMake(0.5*self.view.frame.size.width, 0.23*self.view.bounds.size.height); 
관련 문제