UITableViewCell 在 UINavigationController 应用程序中换行文本

发布于 2024-11-06 04:53:07 字数 1940 浏览 0 评论 0原文

我有一个基于文本大小的每个单元格具有不同高度的表格视图。 我在委托方法 -tableView:heightForRowAtIndexPath 中计算行大小,并分配给数据源方法 -tableView:cellForRowAtIndexPath 中的文本。 我能够根据文本实现适当的高度,但无法使文本环绕;相反,它只是从屏幕上消失。我正在使用基于导航的应用程序使用笔尖在子视图控制器中生成表格视图。我尝试了一些表格视图选项,但无法使文本换行。我希望看到下一个换行就像它是一个文本视图一样,但我希望整个内容显示在单元格中,而表格单元格本身没有滚动条。这可能吗?

这是更新后的代码,高度正在正确调整自身,但单元格没有自动换行...

-

(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *string = [self.quizDictionary objectForKey:[_temp objectAtIndex:testKV]];

    CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15]
                          constrainedToSize:CGSizeMake(320, 9999) 
                              lineBreakMode:UILineBreakModeWordWrap];
    return stringSize.height+25;
} 


- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int row = indexPath.row;
    if (row > 6) {
        return nil;
    }
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:CellIdentifier] 
                autorelease];
        } 
    // configure the cell...
    NSString *string = [self getQuestionPart];

    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:textV];
    [textV release];
    testKV++; 
    return cell;
}

I am having a tableview with different heights for each cell based on the size of the text.
I calculate the rowsize in the delegate method -tableView:heightForRowAtIndexPath, and assign to the text in the datasource method -tableView:cellForRowAtIndexPath.
I am able to realize the appropriate height based on the text, but am not able to make the text wrap around; instead it just disappears off the screen. I am using a navigation-based app to generate the tableview in the child view controller using a nib. I played around with some of the options for tableview, but I can't make the text wrap. I would like to see the next wrap as if it were a textview, but I want the entire content displayed in the cell, without scroll bars in the table cell itself. Is this possible?

Here is the updated code, the height is adjusting itself correctly, but the cell is not word wrapping...

-

(CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *string = [self.quizDictionary objectForKey:[_temp objectAtIndex:testKV]];

    CGSize stringSize = [string sizeWithFont:[UIFont boldSystemFontOfSize:15]
                          constrainedToSize:CGSizeMake(320, 9999) 
                              lineBreakMode:UILineBreakModeWordWrap];
    return stringSize.height+25;
} 


- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int row = indexPath.row;
    if (row > 6) {
        return nil;
    }
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:CellIdentifier] 
                autorelease];
        } 
    // configure the cell...
    NSString *string = [self getQuestionPart];

    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:textV];
    [textV release];
    testKV++; 
    return cell;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨的味道风的声音 2024-11-13 04:53:07

您想要:

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

让文本在单元格内换行。这与仔细实现 tableView:heightForRowAtIndexPath: 相结合应该可以解决问题。

You want:

cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

to get your text to wrap within the cell. This in combination with a careful implementation of tableView:heightForRowAtIndexPath: should do the trick.

乜一 2024-11-13 04:53:07

是的,这是可能的!

看到这里,你需要有点技巧。您需要动态计算textView的高度,并根据TextView的高度,您需要返回单元格的高度。

这是您可以计算字符串大小的代码....

首先获取String & 的大小根据字符串设置单元格的高度

- (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;
}

Yes it is possible!!!

Looking at this,you need to be somewhat tricky. You need to calculate the height of the textView dynamically and based on the Height of the TextView,you need to return the Height for the cell..

This is the code by which you can calculate the size of string....

First get the size of String & set the height of the cell based on string

- (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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文