2011-08-15 2 views
2

주소 셀에 가변 높이가 필요합니다.가변 높이로 UITableViewCell을 그릴 때 잘못된 너비 가져 오기

viewWillAppear에서 저는 라벨 높이를 ivar로 캡처하기 위해 테스트 셀을 그립니다.

대표 메서드 인 tableView:heightForRowAtIndexPath에서 실제 주소 셀의 높이를 설정하려면 ivar를 사용합니다.

코드 :

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 

UITableViewCell *testCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:@"addressCell"] autorelease]; 

testCell.textLabel.text = @"Address"; 
testCell.detailTextLabel.text = [selectedPatient valueForKey:@"address"]; 
testCell.selectionStyle = UITableViewCellSelectionStyleNone; 
testCell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap; 
testCell.frame = CGRectMake(0,0,100,30); 
[testCell addSubview:testCell.detailTextLabel]; 
[testCell sizeToFit]; 

CGSize maxSize; 

// HERE is where the problem occurs 
// maxSize.width is 35 when it should be 200 
maxSize.width = testCell.detailTextLabel.frame.size.width; 
// 

maxSize.height = 500; 

UIFont *font = testCell.detailTextLabel.font; 

CGSize testSize = [[selectedPatient valueForKey:@"address"] sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; 

addressLabelHeight = testSize.height; 

[testCell removeFromSuperview]; 
} 

테스트 셀의 폭을 얻기는 문제입니다. NSLog'ing cellForRowAtIndexPath: 때 너비가 200 수 있지만 내 testCell 너비를 35 표시합니다. 이로 인해 너무 큰 공백을 너무 많은 공백을 보여주는 높이가됩니다.

maxSize.width = 200을 설정하여 문제를 해결했지만 detailLabel의 너비를 동적으로 얻으려는 것이 잘못된 것인지 궁금합니다.

답변

2

maxSize.width은 텍스트의 너비가 될 것입니다. @"address"에서 얻은 텍스트는 무엇입니까? 나는 약 35 포인트의 넓이를 내기도한다.

BTW, 왜 이러는 :

// This is legal, but a strange size 
testCell.frame = CGRectMake(0,0,100,30); 

// This is redundant I believe 
[testCell addSubview:testCell.detailTextLabel]; 

// Does this help you at all? 
[testCell sizeToFit]; 

당신은 정말 tableView:willDisplayCell:atIndexPath: 때까지 셀의 폭을 믿을 수 없다. 그것이 최종 레이아웃을하고 싶은 곳입니다. 목적에 따라 원하는 주소 (원하는 경우 200 포인트)를 얼마나 넓게 선택했는지를 물어 보는 대신 폭을 지정해야합니다.

+0

tableView : willDisplayCell : atIndexPath에 대해 몰랐습니다. 감사! CGRectMake로 셀의 프레임을 만들지 않고도 치수는 0이됩니다. Cocoa로 그리기에 좋은 리소스를 추천 할 수 있습니까? –

+0

초기 치수는 예, 기본 프레임이므로 CGRectZero가됩니다. 발신자가 뭔가를 제공해야합니다. 일반적으로 그것이 얼마나 넓은지를 아는 유일한 것인'UITableView'에 의해 수행됩니다. 이와 같은 레이아웃 문제의 경우 View Programming Guide가 필요합니다. 실제 "드로잉"을 위해서는 코코아 드로잉 가이드가 필요합니다. [iOS 5 프로그래밍 : 한계 극복] (http://www.wiley.com/WileyCDA/WileyTitle/productCd-1119961327.html)은 올해 하반기에 나올 예정이며 그리기 최적화에 많은 페이지를 투자합니다. –

관련 문제