2

I would like to create a blog part of an app using a UITableView. The table should have two sections and a grouped style. The first section is the blog and the second section of the table, comments. Blogs can be any size. When entering large amount of text, and by this I mean 10K words or more, with links or/and photos, the app crashes or does not display the text at all. Comments can also have a large amount of text as well as links and photos. It seems that UITableView has its limitations. These are the problems I am facing:

  1. Memory
  2. Does not always display all the text (if it is too long)
  3. Scrolling lags

Any ideas on how to accomplish this? Should I try creating a table of my own with UITextViews to try to mimic the behaviour of the UITableView? Is there an easy way to implement it with Apple's UITableView? I have not seen an app out there that does this. Any code that can help me get started? I also have to use NSAttributedString for text formatting.

UPDATE: Reading the documentation for tableView:heightForRowAtIndexPath: found an important note which answers the question but not my problem.

Important: Due to an underlying implementation detail, you should not return values greater than 2009.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
GianPac
  • 462
  • 1
  • 7
  • 18

2 Answers2

1

you need to do dynamically adjust your table view cell height according to your text.After that you need to create a label with dynamic height that depends on the text and add that label in cell.

For memory management you need to make sure that your cell is auto release and the sub views that you add in cell,also get relaese.

Here is some code that will help you in displaying your whole text without headache of scrolling.

Assuming blogArray is the array that contains text that you want to display in cell.(replace with your own).

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if([self.blogArray count]>0)
    {
    CGSize labelsize;
    UILabel *blogTextLabel = [[UILabel alloc] init];;
    [blogTextLabel setNumberOfLines:0];
    [blogTextLabel setBackgroundColor:[UIColor clearColor]];
    NSString *text=[self.blogArray objectAtIndex:indexPath.row];
    [blogTextLabel setFont:[UIFont fontWithName:@"Helvetica"size:14]];
    labelsize=[text sizeWithFont: blogTextLabel.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    blogTextLabel.frame=CGRectMake(10, 24, 268, labelsize.height);
    blogTextLabel.text=text;
    [cell.contentView addSubview: blogTextLabel];
    [blogTextLabel release];


    }
    else {
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}

You also need to adjust your table view cell height :-

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

{
    CGSize labelsize;
    UILabel * textDesc1 = [[UILabel alloc] init];
    [textDesc1 setNumberOfLines:0];
    textDesc1.text=[self.blogArray objectAtIndex:indexPath.row];
    [textDesc1 setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
    labelsize=[textDesc1.text sizeWithFont:textDesc1.font constrainedToSize:CGSizeMake(268, 2000.0) lineBreakMode:UILineBreakModeWordWrap];
    labelsize.height=labelsize.height+35;
    [textDesc1 release];
    return (CGFloat)labelsize.height; 


} 
Gypsa
  • 11,230
  • 6
  • 44
  • 82
  • I do not think you put the code to test on a device. First, you are adding an extra label to the already existing label on contentview, so that is not going to help with memory. Have you tried using more than 10,000 words on a couple of cells? I know how tables work, how to resize them dynamically, how to assign attributes to strings, etc. But not how to mix it all together so that scrolling is smooth and most important without crashing the app. Try it and get back to me if you have a solution. Thanks – GianPac Nov 23 '11 at 14:22
0

Make sure that you are reusing cells inside your cellForRowAtIndexPath: datasource method. That should ensure that the scrolling doesn't lag. If it continues to lag, make sure you're not blocking the main thread as you draw your cells. Also, you can use properties like adjustsFontSizeToFitWidth and lineBreakMode in a UILabel to control how much text appears at any given time.

Make sure you autorelease any cells you alloc in cellForRowAtIndexPath: too.

Community
  • 1
  • 1
Alan Zeino
  • 4,406
  • 2
  • 23
  • 30