Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-09-2009, 09:20 AM   #1 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
chuck is on a distinguished road
Default 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!
__________________
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
chuck is offline   Reply With Quote
Old 04-09-2009, 10:30 AM   #2 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
PhoneyDeveloper is on a distinguished road
Default

The TableViewSuite example code shows several ways to implement multiline cells. Adding a label would seem to be the simplest.
PhoneyDeveloper is offline   Reply With Quote
Old 04-09-2009, 11:39 AM   #3 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
chuck is on a distinguished road
Default

Quote:
Originally Posted by PhoneyDeveloper View Post
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!
__________________
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
chuck is offline   Reply With Quote
Old 04-09-2009, 12:02 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Location: Toronto, ON
Posts: 111
CanadaDev is on a distinguished road
Default

Do you have the Beginning iPhone Development book? Page 200 has a good tutorial for adding more lines to a row through subviews.
CanadaDev is offline   Reply With Quote
Old 04-09-2009, 02:00 PM   #5 (permalink)
New Member
 
Join Date: Mar 2009
Posts: 3
skyephone is on a distinguished road
Default

Quote:
Originally Posted by CanadaDev View Post
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.
skyephone is offline   Reply With Quote
Old 04-09-2009, 02:13 PM   #6 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
chuck is on a distinguished road
Default

Quote:
Originally Posted by CanadaDev View Post
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!
__________________
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
chuck is offline   Reply With Quote
Old 04-09-2009, 02:15 PM   #7 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 1,431
PhoneyDeveloper is on a distinguished road
Default

Yowza, throw that code away.

Use the code from this thread to load a cell from a nib

http://www.iphonedevsdk.com/forum/ip...-cell-nib.html
PhoneyDeveloper is offline   Reply With Quote
Old 04-13-2009, 05:07 PM   #8 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 1
sahildesai is on a distinguished road
Default Always do everything programmatically, why? Because um just because.

Quote:
Originally Posted by chuck View Post
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.
sahildesai is offline   Reply With Quote
Old 04-15-2009, 12:08 PM   #9 (permalink)
Registered Member
 
chuck's Avatar
 
Join Date: Aug 2008
Location: Berlin, Germany
Posts: 87
chuck is on a distinguished road
Default 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.
__________________
German Course: Learn German anywhere! Chess Player: Study Chess anywhere!
chuck is offline   Reply With Quote
Old 05-17-2011, 07:56 PM   #10 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 1
jeffbonta is on a distinguished road
Default

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?
jeffbonta is offline   Reply With Quote
Old 05-17-2011, 08:35 PM   #11 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

Code:
cell.textLabel.numberOfLines = 0;
architectpianist is offline   Reply With Quote
Old 12-13-2011, 07:16 PM   #12 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 7
rellinb is on a distinguished road
Default

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 View Post
Code:
cell.textLabel.numberOfLines = 0;
rellinb is offline   Reply With Quote
Reply

Bookmarks

Tags
multiline, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 332
7 members and 325 guests
blueorb, guusleijsten, jbro, Kryckter, LEARN2MAKE, n00b, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0