Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

best way to make multiline UITableViewCell?

chuckchuck Posts: 87Registered Users
edited December 2011 in iPhone SDK Development
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
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!
Post edited by chuck on
<a href="http://www.itunes.com/app/GermanCourse" target="_blank">German Course</a>: Learn German anywhere! <a href="http://www.itunes.com/app/ChessPlayer" target="_blank">Chess Player</a>: Study Chess anywhere!
· ·

Replies

  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    edited April 2009
    The TableViewSuite example code shows several ways to implement multiline cells. Adding a label would seem to be the simplest.
    · ·
  • chuckchuck Posts: 87Registered Users
    edited April 2009
    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!
    <a href="http://www.itunes.com/app/GermanCourse" target="_blank">German Course</a>: Learn German anywhere! <a href="http://www.itunes.com/app/ChessPlayer" target="_blank">Chess Player</a>: Study Chess anywhere!
    · ·
  • CanadaDevCanadaDev Posts: 111Registered Users
    edited April 2009
    Do you have the Beginning iPhone Development book? Page 200 has a good tutorial for adding more lines to a row through subviews.
    · ·
  • skyephoneskyephone Posts: 3New Users
    edited April 2009
    CanadaDev wrote: »
    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
    - (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.
    · ·
  • chuckchuck Posts: 87Registered Users
    edited April 2009
    CanadaDev wrote: »
    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!
    <a href="http://www.itunes.com/app/GermanCourse" target="_blank">German Course</a>: Learn German anywhere! <a href="http://www.itunes.com/app/ChessPlayer" target="_blank">Chess Player</a>: Study Chess anywhere!
    · ·
  • PhoneyDeveloperPhoneyDeveloper Posts: 1,431Registered Users
    edited April 2009
    Yowza, throw that code away.

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

    http://www.iphonedevsdk.com/forum/iphone-sdk-development/12550-how-load-table-view-cell-nib.html
    · ·
  • sahildesaisahildesai Posts: 1New Users
    edited April 2009
    chuck wrote: »
    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.
    · ·
  • chuckchuck Posts: 87Registered Users
    edited April 2009
    Just wanted you all to know I got this working by using the following 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. :)
    <a href="http://www.itunes.com/app/GermanCourse" target="_blank">German Course</a>: Learn German anywhere! <a href="http://www.itunes.com/app/ChessPlayer" target="_blank">Chess Player</a>: Study Chess anywhere!
    · ·
  • jeffbontajeffbonta Posts: 1New Users
    edited May 2011
    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?
    · ·
  • architectpianistarchitectpianist Posts: 130Registered Users
    edited May 2011
    cell.textLabel.numberOfLines = 0;
    
    · ·
  • rellinbrellinb Posts: 7New Users
    edited December 2011
    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.
    cell.textLabel.numberOfLines = 0;
    
    · ·
Sign In or Register to comment.