简体   繁体   中英

Tableview - Title overlapping Detail (Swift)

The image attached says it all. Is there any way to prevent the title from overlapping such as automatically moving to a second line whenever it reaches its limit? programatically/through interface builder.

PS: im retrieving the names from firebase database. this is a voting page and title are the booth names

TableView Controller

import UIKit
import FirebaseDatabase

var pitchData = [String]()
var myIndex = 0
class PitchTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.setNavigationBarHidden(false, animated: true)

        self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedStringKey.font: UIFont(name: "Gotham Rounded", size: 19)!]
        ref = Database.database().reference() //set the firebase reference
        // Retrieve the post and listen for changes
        databaseHandle = ref?.child("Afternoon13").queryOrdered(byChild: "BoothPosition").observe(.value, with: { (snapshot) in
            pitchData.removeAll()

            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let key = snap.key


                pitchData.append(key)

            }
            self.tableView.reloadData()
        })
    }

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

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return pitchData.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.font = UIFont(name: "Gotham Rounded", size: 20)
        cell.detailTextLabel?.font = UIFont(name: "Gotham Rounded", size: 20)
        cell.textLabel?.text = pitchData[indexPath.row]

        switch (pitchData[indexPath.row]) {
        case "ARC - attract, retain and cultivate":
            let returnValue: Int = UserDefaults.standard.integer(forKey: "voting")
            if (returnValue == 1)
            {
                cell.detailTextLabel?.text = "Completed ✓"
                cell.detailTextLabel?.textColor = UIColor.green
                cell.isUserInteractionEnabled = false
            }
            else {
                cell.detailTextLabel?.text = "Vote ᐳ"
                cell.detailTextLabel?.textColor = UIColor.blue
            }
        case "Anytime RCs":
            let returnValue: Int = UserDefaults.standard.integer(forKey: "voting1")
            if (returnValue == 1)
            {
                cell.detailTextLabel?.text = "Completed ✓"
                cell.detailTextLabel?.textColor = UIColor.green
                cell.isUserInteractionEnabled = false
            }
            else {
                cell.detailTextLabel?.text = "Vote ᐳ"
                cell.detailTextLabel?.textColor = UIColor.blue
            }

在此处输入图片说明

You need to set lines to 0:

在此处输入图片说明

You can set it directly from StoryBoard:

or you can set Programmatically:

cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0

Hope this Help.

Edit : Demo of the TableView Constraints

I have taken two labels in the cell next to each other:

This is my Output of TableView:

在此处输入图片说明

This is constraint of label First:

在此处输入图片说明

This is the Constraints of label 2:

在此处输入图片说明

And yes it worked for me, The text is multi line with no text cutting, please double Check your constraints with this.

Edit To see the Constraints

Select the label and then Select the Show the Size Inspector:

Scroll Down and you can see the given constraints.

在此处输入图片说明

Edit 2

As per you description you have Auto resizing:

So you can use the following :

For label One

在此处输入图片说明

For label second:

在此处输入图片说明

Since you are using default UITableViewCell 's textLabel , this should fix everything :

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

You set your UILabel property numberOfLines = 0

在此处输入图片说明

  1. set UILabel Top , bottom , leading , trailing Constraints .

在此处输入图片说明

  1. add UITableViewDelegate method.

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } 
  2. also set your vote Constraints like this.

在此处输入图片说明

  1. select your vote UIButton set Constraints

    1. step set Top , bottom , leading , trailing and fix width.

在此处输入图片说明

  1. step set vertically center Constraints

在此处输入图片说明

Make sure three things:-

  • Title label has bottom, top and leading constraint with cell content view as well as horizontal spacing with Vote >, and Vote > button has all constraint like top bottom leading and horizontal space with title.
  • Title label has set numberOfLines = 0
  • Set dynamic height of cell using UITableViewAutomaticDimension in heightForRowAt

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } 

Let try with code

// in view did load:
self.tableView.rowHeight = UITableViewAutomaticDimension

// cell row at indexpath
let reuseIdentifier = "cell"
        var cell:UITableViewCell? =
            tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        if (cell == nil)
        {
            cell = UITableViewCell(style: .value1,
                                   reuseIdentifier: reuseIdentifier)
        }

        // your customize font, color... here

        cell!.textLabel?.numberOfLines = 0;
        cell!.detailTextLabel?.numberOfLines = 0;

        return cell!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM