Priyal Priyal - 4 years ago 104
iOS Question

How to Show Multiple Lines in a Table View Cell

I am trying to building a to-do-list app. My StoryBoard consists of 2 view controller connected via push Segue. First ViewController is a table View Controller and second is a View Controller. Second View Controller consists a Title, description and date field, data is fetched from second View Controller and displayed in First View Controller. Data is received in form of an object say taskObject which has three attributes (Title, description and date) for three text fields.

I want to show have a cell Like Below:

Buy Notebook (Title Text)

2 with both side rule pages, 1 with single side ruled (Description Text)

27/07/2015 (Date Text)

I have already been through:

  1. Display multiple lines in a Table view's cell - IOS

  2. How to display multiple lines in a tableView cell

Just to be clear. Both are not my case.

I am trying to display like this :

cell.textLabel.text = [[self.allTask objectAtIndex:indexPath.row] titleText] ;
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.Text = [self.allTask[indexPath.row] descriptionText];
cell.detailTextLabel.Text =[[self.allTask objectAtIndex:indexPath.row] dat

But it is not working, only last cell.textLabel.text's data is visible.

I did add:

cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;

But it is helpful ONLY if U want to show multiple lines from a single data source (NOT my case). In My situation have have to show three data from three different data attributes in three different lines within a cell.

I know it is possible through custom cells. Is their any other way to it ?
Any Help Is Appreciated.

Answer Source

If you do

cell.textLabel.text = [[self.allTask objectAtIndex:indexPath.row] titleText] ;
cell.textLabel.text = [[self.allTask objectAtIndex:indexPath.row] descriptionText];
cell.textLabel.text = [[self.allTask objectAtIndex:indexPath.row] dateText];

you are effectively overwriting the textLabel each time. A standard UITableViewCell has another label, the detailTextLabel (if its style is not Default). But three labels is impossible without using custom cells. The best you can achieve is to use a multiline label, like this:

Task *task = (Task *)[self.allTask objectAtIndex:indexPath.row];
cell.textLabel.text = [task titleText];
cell.detailTextLabel.numberOfLines = 0; // 0 means 'no limit'
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\n%@",
    [task descriptionText], [task dateText]];

You might need to adjust the tableview's rowHeight, though.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download