이것은 iPhone 0S 2.0에 있습니다. 2.1에 대한 답변도 훌륭하지만 테이블과 관련된 차이점을 알지 못합니다.
기본적으로 UITableViewCell
포함되어 있기 때문에 사용자 정의 셀을 만들지 않고 텍스트를 줄 바꿈하는 것이 가능해야합니다 UILabel
. 맞춤 셀을 만들면 작동 할 수 있지만 이것이 달성하려는 것이 아닙니다. 현재 접근 방식이 작동하지 않는 이유를 이해하고 싶습니다.
셀이 텍스트 및 이미지 액세스를 지원하므로 필요할 때까지 데이터보기를 만들지 않기 때문에 필요에 따라 레이블이 생성된다는 것을 알았습니다. 그래서 다음과 같이하면 :
cell.text = @""; // create the label
UILabel* label = (UILabel*)[[cell.contentView subviews] objectAtIndex:0];
유효한 레이블이 있지만 numberOfLines
그 (및 lineBreakMode) 설정이 작동하지 않습니다. 여전히 한 줄 텍스트가 나타납니다. UILabel
텍스트가 표시되는 높이가 충분합니다. 높이 의 큰 값을 반환합니다 heightForRowAtIndexPath
.
답변
다음은 더 간단한 방법이며 나를 위해 작동합니다.
당신의 cellForRowAtIndexPath:
기능 안에서 . 셀을 처음 만들 때 :
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}
레이블의 줄 수를 0으로 설정했습니다. 그러면 필요한만큼 줄을 사용할 수 있습니다.
다음 부분은 얼마나 큰지 지정하는 UITableViewCell
것입니다. heightForRowAtIndexPath
함수 에서 그렇게하십시오 .
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = @"Go get some text for your cell.";
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 20;
}
텍스트 주위에 작은 버퍼가 있기 때문에 반환 된 셀 높이에 20을 추가했습니다.
답변
iOS7에 대한 Tim Rupe의 답변을 업데이트했습니다.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = @"Go get some text for your cell.";
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
NSAttributedString *attributedText =
[[NSAttributedString alloc]
initWithString:cellText
attributes:@
{
NSFontAttributeName: cellFont
}];
CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(tableView.bounds.size.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
context:nil];
return rect.size.height + 20;
}
답변
같은 문제가있을 때 내 경험을 기록하기위한 간단한 의견 / 답변. 코드 예제를 사용했지만 테이블 뷰 셀 높이가 조정되었지만 셀 내부의 레이블이 여전히 올바르게 조정되지 않았습니다. 해결책은 셀 높이가 조정 된 후 사용자 정의 NIB 파일에서 셀을로드하는 것 입니다.
그리고 텍스트를 줄 바꿈하지 않고 NIB 파일 내에서 설정을 지정했으며 레이블에 한 줄만 있습니다. NIB 파일 설정이 코드 내에서 조정 한 설정을 재정의했습니다.
제가받은 교훈은 각 시점에서 물체의 상태를 항상 명심해야한다는 것입니다. 아직 만들어지지 않았을 수도 있습니다! … 줄을서는 사람.
답변
UITableView
셀에 텍스트 만 추가 해야하는 경우 작업 할 델리게이트가 두 명만 필요합니다 (추가 할 필요 없음 UILabels
).
1) cellForRowAtIndexPath
2) heightForRowAtIndexPath
이 솔루션은 저에게 효과적이었습니다.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
cell.textLabel.text = [mutArr objectAtIndex:indexPath.section];
NSLog(@"%@",cell.textLabel.text);
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png" ]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize labelSize = CGSizeMake(200.0, 20.0);
NSString *strTemp = [mutArr objectAtIndex:indexPath.section];
if ([strTemp length] > 0)
labelSize = [strTemp sizeWithFont: [UIFont boldSystemFontOfSize: 14.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
return (labelSize.height + 10);
}
여기 문자열 mutArr
은 데이터를 얻는 가변 배열입니다.
편집 :- 여기 내가 찍은 배열입니다.
mutArr= [[NSMutableArray alloc] init];
[mutArr addObject:@"HEMAN"];
[mutArr addObject:@"SUPERMAN"];
[mutArr addObject:@"Is SUPERMAN powerful than HEMAN"];
[mutArr addObject:@"Well, if HEMAN is weaker than SUPERMAN, both are friends and we will never get to know who is more powerful than whom because they will never have a fight among them"];
[mutArr addObject:@"Where are BATMAN and SPIDERMAN"];
답변
이제 테이블 뷰에 자체 크기 셀이있을 수 있습니다. 다음과 같이 테이블보기를 설정하십시오.
tableView.estimatedRowHeight = 85.0 //use an appropriate estimate
tableView.rowHeight = UITableViewAutomaticDimension
답변
다음 솔루션을 사용합니다.
데이터는 회원에게 별도로 제공됩니다.
-(NSString *)getHeaderData:(int)theSection {
...
return rowText;
}
에서 쉽게 처리 할 수 있습니다 cellForRowAtIndexPath
. 셀을 정의 / 글꼴을 정의하고이 값을 결과 “셀”에 지정하십시오. numberoflines
이 “0”으로 설정되어 필요한 것을 가져 옵니다 .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
cell.textLabel.text= [self getRowData:indexPath.section];
cell.textLabel.font = cellFont;
cell.textLabel.numberOfLines=0;
return cell;
}
에서는 heightForRowAtIndexPath
, I은, 랩 된 텍스트의 높이를 계산합니다. 보딩 크기는 셀 너비와 관련이 있습니다. iPad의 경우 이것은 1024입니다. iPhone 및 iPod 320의 경우.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIFont *cellFont = [UIFont fontWithName:@"Verdana" size:12.0];
CGSize boundingSize = CGSizeMake(1024, CGFLOAT_MAX);
CGSize requiredSize = [[self getRowData:indexPath.section] sizeWithFont:cellFont constrainedToSize:boundingSize lineBreakMode:UILineBreakModeWordWrap];
return requiredSize.height;
}
답변
나는 이것이 매우 간단하고 간단하다는 것을 알았습니다.
[self.tableView setRowHeight:whatEvereight.0f];
예를 들어 :
[self.tableView setRowHeight:80.0f];
이것은 최선의 / 표준 접근 방식 일 수도 있고 아닐 수도 있지만 내 경우에는 효과가있었습니다.