Reputation: 492
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:
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.
Upvotes: 2
Views: 2818
Reputation: 11314
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;
}
Upvotes: 1
Reputation: 4396
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.
Upvotes: 0