[英]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输出:
这是标签First的约束:
这是标签2的约束:
是的,它对我有用,文本是多行,没有文本剪切,请仔细检查您的约束。
选择标签,然后选择“显示尺寸检查器”:
向下滚动,您可以看到给定的约束。
根据您的描述,您具有自动调整大小:
因此,您可以使用以下代码:
对于标签一
对于第二标签:
由于您使用的是默认的UITableViewCell
的textLabel ,因此应该可以解决所有问题:
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = .byWordWrapping;
确保三件事:
使用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.