UITableViewCell在UINavigationController应用程序中换行文本
我有一个基于文本大小的表格视图,每个单元格有不同的高度。 我在委托方法UITableViewCell在UINavigationController应用程序中换行文本,uitableview,uinavigationcontroller,Uitableview,Uinavigationcontroller,我有一个基于文本大小的表格视图,每个单元格有不同的高度。 我在委托方法-tableView:heightForRowAtIndexPath中计算行大小,并在数据源方法-tableView:cellForRowAtIndexPath中分配给文本。 我能够根据文本实现适当的高度,但无法使文本环绕;相反,它只是从屏幕上消失。我正在使用基于导航的应用程序在子视图控制器中使用nib生成tableview。我使用了tableview的一些选项,但是我不能使文本换行。我希望看到下一个换行,就像它是一个文本视图
-tableView:heightForRowAtIndexPath
中计算行大小,并在数据源方法-tableView:cellForRowAtIndexPath
中分配给文本。
我能够根据文本实现适当的高度,但无法使文本环绕;相反,它只是从屏幕上消失。我正在使用基于导航的应用程序在子视图控制器中使用nib生成tableview。我使用了tableview的一些选项,但是我不能使文本换行。我希望看到下一个换行,就像它是一个文本视图一样,但我希望整个内容都显示在单元格中,而表格单元格本身没有滚动条。这可能吗
这是更新后的代码,高度正在正确调整,但单元格没有换行
-
你想要:
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
使文本在单元格中换行。这与仔细执行tableView:heightForRowAtIndexPath:
相结合,应该可以做到这一点。是的,这是可能的
看看这个,你需要有点狡猾。您需要动态计算textView的高度,并根据textView的高度返回单元格的高度
这是您可以用来计算字符串大小的代码
首先获取字符串的大小并根据字符串设置单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];
NSString *label = [d valueForKey:@"Description"];
CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];
return stringSize.height+25;
}
- (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];
//}
NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];
NSString *string = [d valueForKey:@"Description"];
CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
UITextView *textV=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height+10)];
textV.font = [UIFont systemFontOfSize:15.0];
textV.text=string;
textV.textColor=[UIColor blackColor];
textV.editable=NO;
[cell.contentView addSubview:twitLbl];
[twitLbl release];
return cell;
}
Ajay,我添加了上面的代码,但它没有包装。我编辑了我的原始问题以添加CellForRowatineXpath和-HeightForRowatineXpath(我必须将TwitBL更改为textV,对吗?),并且必须注释掉textV.editable=no,因为我得到了NSInvalidArgumentException',原因:'-[UILabel setEditable:]:未识别的选择器发送到实例0x4b7cd90
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];
NSString *label = [d valueForKey:@"Description"];
CGSize stringSize = [label sizeWithFont:[UIFont boldSystemFontOfSize:15]
constrainedToSize:CGSizeMake(320, 9999)
lineBreakMode:UILineBreakModeWordWrap];
return stringSize.height+25;
}
- (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];
//}
NSDictionary *d=(NSDictionary *)[self.menuArray objectAtIndex:indexPath.section];
NSString *string = [d valueForKey:@"Description"];
CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15] constrainedToSize:CGSizeMake(320, 9999) lineBreakMode:UILineBreakModeWordWrap];
UITextView *textV=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 290, stringSize.height+10)];
textV.font = [UIFont systemFontOfSize:15.0];
textV.text=string;
textV.textColor=[UIColor blackColor];
textV.editable=NO;
[cell.contentView addSubview:twitLbl];
[twitLbl release];
return cell;
}