繁体   English   中英

Tableview-标题重叠的明细(快速)

[英]Tableview - Title overlapping Detail (Swift)

所附图片说明了一切。 有什么方法可以防止标题重叠,例如,每当标题达到极限时自动移动到第二行? 以编程方式/通过界面构建​​器。

PS:我正在从Firebase数据库中检索名称。 这是投票页,标题是展位名称

TableView控制器

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
            }

在此处输入图片说明

您需要将行设置为0:

在此处输入图片说明

您可以直接从StoryBoard进行设置:

或者您可以通过编程方式设置:

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

希望这个帮助。

编辑:TableView约束的演示

我在单元格中相邻放置了两个标签:

这是我的TableView输出:

在此处输入图片说明

这是标签First的约束:

在此处输入图片说明

这是标签2的约束:

在此处输入图片说明

是的,它对我有用,文本是多行,没有文本剪切,请仔细检查您的约束。

编辑以查看约束

选择标签,然后选择“显示尺寸检查器”:

向下滚动,您可以看到给定的约束。

在此处输入图片说明

编辑2

根据您的描述,您具有自动调整大小:

因此,您可以使用以下代码:

对于标签一

在此处输入图片说明

对于第二标签:

在此处输入图片说明

由于您使用的是默认的UITableViewCelltextLabel ,因此应该可以解决所有问题:

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

您设置UILabel属性numberOfLines = 0

在此处输入图片说明

  1. 设置UILabel顶部,底部,前导,尾随约束

在此处输入图片说明

  1. 添加UITableViewDelegate方法。

     func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension } 
  2. 还可以这样设置您的投票约束

在此处输入图片说明

  1. 选择您的投票UIButton设置约束

    1. 设置顶部,底部,前导,尾随和固定宽度。

在此处输入图片说明

  1. 步骤设置垂直居中约束

在此处输入图片说明

确保三件事:

  • 标题标签具有单元格内容视图的底部,顶部和开头约束,以及具有Vote>的水平间距,并且Vote>按钮具有所有约束,例如带有标题的顶部,底部开头和水平空间。
  • 标题标签已设置numberOfLines = 0
  • 使用heightForRowAt中的UITableViewAutomaticDimension设置单元格的动态高度

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

让我们尝试一下代码

// 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!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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