Emm
Emm

Reputation: 1603

How to add more lines in cells of Table View?

I made an app with Table View and I put some cells, but in Cells there is only one line of text, How can I use more lines ?

This is how it looks.

This is how is required to be.

And this is code that I used:

import UIKit

@available(iOS 9.0, *)
class DuasViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let tableList:[String] = ["Name", "Surname", "Date of Birth", "Place of Birth"]


@IBOutlet weak var myTableView: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.


    myTableView.reloadData()


    func allowMultipleLines(tableViewCell:UITableViewCell) {
        tableViewCell.textLabel?.numberOfLines = 10
        tableViewCell.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
    }

    myTableView.reloadData()
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    var returnValue = 0

    returnValue = tableList.count

    return returnValue
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCellWithIdentifier("myCells", forIndexPath: indexPath)

    myCell.textLabel!.text = tableList[indexPath.row]
    return myCell
}  
}

So how I'm gonna add more two lines, and Lines needs to be different, for ex: Name, Stevens, Steve. Surname, My Surname Is, Jobs. Date of Birth, My Date of birth is, 01/01/2016.

Thanks for your contribution.

Upvotes: 0

Views: 2725

Answers (2)

vihar sheladia
vihar sheladia

Reputation: 54

For new iOS 8 there is a new way to make this simple.

There is new a parameter that calculates your cell height with respect of your auto layout constraints. This is UITableViewAutomaticDimension. You can use it in the viewWillAppear method of your view controller.

Objective C:

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.tableView.estimatedRowHeight = 70.0; // for example. Set your average height 
  self.tableView.rowHeight = UITableViewAutomaticDimension;
  [self.tableView reloadData];
}

Swift:

override func viewWillAppear(animated: Bool) {
    self.tableView.estimatedRowHeight = 70 // for example. Set your average height 
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.reloadData()

} 

Work nice and as you want at your example. As for me, I add height things in viewDidLoad and left in viewWillAppear only reloadData(). There is also useful source.

From documentation: The default value of rowHeight is UITableViewAutomaticDimension. I leave the code as it is, for now, but keep in mind that you don't need to set row height in iOS 8+.

Upvotes: 1

EmilioPelaez
EmilioPelaez

Reputation: 19922

The numberOfLines property of UILabel defaults to 1, set it to 0 to allow unlimited lines.

So in your cellForRowAtIndexPath method you'd have

myCell.textLabel!.numberOfLabels = 0

If you want Surname, My Surname Is, Jobs to be in different lines, you have to use a line break, which is \n. Your string would look like this Surname\nMy Surname Is\nJobs

Upvotes: 3

Related Questions