04-09-2009, 09:20 AM
#1 (permalink )
Registered Member
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
best way to make multiline UITableViewCell?
I've been looking all over various forums and in my iPhone dev books, but I can't seem to find a good solution for this. I'd like to run the following code
Code:
cell.text = @"First line\nSecond line";
but this just puts everything on the same line. Is there a simple way to display text this multiline or will I have to do something like subclassing the UITableViewCell or putting a UILabel inside this cell with a frame? Each of my table views cells will have exactly five lines. Thanks in advance for any help you can provide!
04-09-2009, 10:30 AM
#2 (permalink )
New Member
Join Date: Sep 2008
Posts: 1,431
The TableViewSuite example code shows several ways to implement multiline cells. Adding a label would seem to be the simplest.
04-09-2009, 11:39 AM
#3 (permalink )
Registered Member
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
Quote:
Originally Posted by
PhoneyDeveloper
The TableViewSuite example code shows several ways to implement multiline cells. Adding a label would seem to be the simplest.
Thanks for the tip, I'll check it out!
04-09-2009, 12:02 PM
#4 (permalink )
Registered Member
Join Date: Mar 2009
Location: Toronto, ON
Posts: 111
Do you have the Beginning iPhone Development book? Page 200 has a good tutorial for adding more lines to a row through subviews.
04-09-2009, 02:00 PM
#5 (permalink )
New Member
Join Date: Mar 2009
Posts: 3
Quote:
Originally Posted by
CanadaDev
Do you have the Beginning iPhone Development book? Page 200 has a good tutorial for adding more lines to a row through subviews.
Here is some of the code
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = @"CustomCellIdentifier ";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellView"
owner:self options:nil];
#ifdef __IPHONE_2_1
cell = (CustomCell *)[nib objectAtIndex:0];
#else
cell = (CustomCell *)[nib objectAtIndex:1];
#endif
}
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.computers objectAtIndex:row];
cell.colorLabel.text = [rowData objectForKey:@"Color"];
cell.nameLabel.text = [rowData objectForKey:@"Name"];
return cell;
}
They created a CustomCell class that is a subclass of UITableViewCell which has 4 UILabels(2 on each line). Then it's just a matter of creating a CustomCell in cellForRowAtIndexPath and populating it with the data you want.
Hope this helps.
04-09-2009, 02:13 PM
#6 (permalink )
Registered Member
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
Quote:
Originally Posted by
CanadaDev
Do you have the Beginning iPhone Development book? Page 200 has a good tutorial for adding more lines to a row through subviews.
Yes, I do have it and I thought I had seen an example of that in one of my iPhone books before! That's exactly what I need, thank you so much!
04-13-2009, 05:07 PM
#8 (permalink )
New Member
Join Date: Jan 2009
Posts: 1
Always do everything programmatically, why? Because um just because.
Quote:
Originally Posted by
chuck
Yes, I do have it and I thought I had seen an example of that in one of my iPhone books before! That's exactly what I need, thank you so much!
Don't load from a nib, it just seems slow and blah! Just don't do it. Do it this way instead:
marc hoffman - Displaying Variably-Sized Text Cells in a UITableView
Also take a look at Loren Brichter's method for fastscrolling, bcos it doesn't make sense to not have fast scrolling:
Fast Scrolling in Tweetie with UITableView
I had a mix of the two and added a bunch of customizations and gradient etc.
Enjoy.
04-15-2009, 12:08 PM
#9 (permalink )
Registered Member
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
got it working!
Just wanted you all to know I got this working by using the following code:
Code:
#define ROW_HEIGHT 110
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog( @"Setting table text." );
static NSString *CellIdentifier = @"Transaction";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell addSubview:[[UILabel alloc] initWithFrame:CGRectMake(40.0, 0.0, 280.0, ROW_HEIGHT - 1)]];
}
NSUInteger row = [indexPath row];
NSLog( @"Table cell text: %@", [[transactionHistory objectAtIndex:row] description] );
UILabel *labelText = [[cell subviews] lastObject];
labelText.text = [[transactionHistory objectAtIndex:row] description];
labelText.font = [UIFont systemFontOfSize:14];
labelText.lineBreakMode = UILineBreakModeWordWrap;
labelText.numberOfLines = 5;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return ROW_HEIGHT;
}
Now my only problem is that the cells will highlight when touched, but I can't find a way to disable that. Also, I can't seem to figure out how to refresh the table contents, but I'm sure I'll figure it out.
05-17-2011, 07:56 PM
#10 (permalink )
Registered Member
Join Date: May 2011
Posts: 1
Unfortunately, the above code has a problem. It seems to work until the first cell is reused. Its kind of odd. Prior to cells being reused, the line:
UILabel *labelText = [[cell subviews] lastObject];
creates a UILabel pointer in labelText. When the first reuse occurs, the execution of that line does not create a UILabel pointer. Rather, labelText is a UIImageView pointer. Any thoughts?
05-17-2011, 08:35 PM
#11 (permalink )
Registered Member
Join Date: Feb 2011
Posts: 122
Code:
cell.textLabel.numberOfLines = 0;
12-13-2011, 07:16 PM
#12 (permalink )
Registered Member
Join Date: Dec 2011
Posts: 7
Were you ever able to get this working? I'm seeing a crash when I scroll like you mentioned. I tried the line below, but still no luck.
Quote:
Originally Posted by
architectpianist
Code:
cell.textLabel.numberOfLines = 0;
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl