在 swift 中以编程方式将单元格 textLabel 对齐到左上角
Align cell textLabel to top left programmatically in swift
我使用 swift 中的部分以编程方式创建了一个 TableView。部分和行的数量将始终相同,因为它们只是显示我从 Firebase 提取的信息。
我想更改其中一个单元格的 textLabel 以对齐到顶部,而不是单元格的中间。我希望所有其他文本标签保持居中。有问题的单元格是第 1 部分,第 1 行。
目前看起来像:
我一直在寻找如何做到这一点,很多人都说要做:
cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()
但这不起作用。我将这段代码放在我的 cellForRowAtIndexPath
方法中。
if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0 // HERE
cell.textLabel?.sizeToFit() // HERE
}
}
没有任何反应,文本仍然与单元格中心对齐。
有人知道这样做的简单方法吗?最好不必创建自定义单元格(因为这只需要影响 1 行)?谢谢
更新 - cellForRowAtIndexPath
的所有代码
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: .Value1, reuseIdentifier: cellId)
cell.selectionStyle = .None
cell.textLabel?.font = UIFont(name: "Verdana", size: 14)
cell.textLabel?.textColor = UIColor.darkGrayColor()
cell.detailTextLabel?.font = UIFont(name: "Verdana", size: 14)
if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Share"
}
} else if indexPath.section == 1 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()
}
} else if indexPath.section == 2 {
if indexPath.row == 0 {
cell.textLabel!.text = "UK release date"
cell.detailTextLabel!.text = film?.Date_Released?.capitalizedString
} else if indexPath.row == 1 {
cell.textLabel!.text = "Directed by"
cell.detailTextLabel!.text = film?.Directed_By?.capitalizedString
} else if indexPath.row == 2 {
cell.textLabel!.text = "Running time"
cell.detailTextLabel!.text = film?.Running_Time
} else {
cell.textLabel!.text = "Certificate rating"
cell.detailTextLabel!.text = film?.Certificate_Rating
}
}
return cell
}
cell.textLabel?.sizeToFit()
应该使您的 UILabel 具有包装内容所需的大小。所以你可以做的一件好事是改变背景,看看这条线是否能完成工作。
我想问题是你在单元格中的限制。您应该看看您的代码或故事板。
Autolayout tutorial
注意:你应该编辑你的问题,否则你可能会得到一些否定。如果是section 0就不能是section 1.
if indexPath.section == 0 {
if indexPath.section == 1 {
/*your code */
}
}
cell.textLabel 是 UITableViewCell 中的默认标签。如果您希望您的文本从顶部开始,您应该创建一个 UITableViewCell 的子classe 并为此单元格创建您自己的视图。
例如
import Foundation
import UIKit
class ItemCell: UITableViewCell {
var label : UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.label = UILabel()
self.contentView.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.label.frame = CGRectMake(15,10,200,30)
self.label.adjustsFontSizeToFitWidth = true
self.label.textColor = UIColor.greenColor()
}
}
然后在你的 viewController 中不要忘记像这样使用寄存器 class 方法
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.registerClass(ItemCell.self, forCellReuseIdentifier: "cellIdentifier")
并且在你的 cellForRowAtIndexPath
let cell:ItemCell! = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as? ItemCell
我使用 swift 中的部分以编程方式创建了一个 TableView。部分和行的数量将始终相同,因为它们只是显示我从 Firebase 提取的信息。
我想更改其中一个单元格的 textLabel 以对齐到顶部,而不是单元格的中间。我希望所有其他文本标签保持居中。有问题的单元格是第 1 部分,第 1 行。
目前看起来像:
我一直在寻找如何做到这一点,很多人都说要做:
cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()
但这不起作用。我将这段代码放在我的 cellForRowAtIndexPath
方法中。
if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0 // HERE
cell.textLabel?.sizeToFit() // HERE
}
}
没有任何反应,文本仍然与单元格中心对齐。
有人知道这样做的简单方法吗?最好不必创建自定义单元格(因为这只需要影响 1 行)?谢谢
更新 - cellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: .Value1, reuseIdentifier: cellId)
cell.selectionStyle = .None
cell.textLabel?.font = UIFont(name: "Verdana", size: 14)
cell.textLabel?.textColor = UIColor.darkGrayColor()
cell.detailTextLabel?.font = UIFont(name: "Verdana", size: 14)
if indexPath.section == 0 {
if indexPath.row == 0 {
cell.textLabel!.text = "Share"
}
} else if indexPath.section == 1 {
if indexPath.row == 0 {
cell.textLabel!.text = "Our review"
} else {
cell.textLabel!.text = film?.Main_Review
cell.textLabel?.numberOfLines = 0
cell.textLabel?.sizeToFit()
}
} else if indexPath.section == 2 {
if indexPath.row == 0 {
cell.textLabel!.text = "UK release date"
cell.detailTextLabel!.text = film?.Date_Released?.capitalizedString
} else if indexPath.row == 1 {
cell.textLabel!.text = "Directed by"
cell.detailTextLabel!.text = film?.Directed_By?.capitalizedString
} else if indexPath.row == 2 {
cell.textLabel!.text = "Running time"
cell.detailTextLabel!.text = film?.Running_Time
} else {
cell.textLabel!.text = "Certificate rating"
cell.detailTextLabel!.text = film?.Certificate_Rating
}
}
return cell
}
cell.textLabel?.sizeToFit()
应该使您的 UILabel 具有包装内容所需的大小。所以你可以做的一件好事是改变背景,看看这条线是否能完成工作。 我想问题是你在单元格中的限制。您应该看看您的代码或故事板。 Autolayout tutorial
注意:你应该编辑你的问题,否则你可能会得到一些否定。如果是section 0就不能是section 1.
if indexPath.section == 0 {
if indexPath.section == 1 {
/*your code */
}
}
cell.textLabel 是 UITableViewCell 中的默认标签。如果您希望您的文本从顶部开始,您应该创建一个 UITableViewCell 的子classe 并为此单元格创建您自己的视图。 例如
import Foundation
import UIKit
class ItemCell: UITableViewCell {
var label : UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.label = UILabel()
self.contentView.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.label.frame = CGRectMake(15,10,200,30)
self.label.adjustsFontSizeToFitWidth = true
self.label.textColor = UIColor.greenColor()
}
}
然后在你的 viewController 中不要忘记像这样使用寄存器 class 方法
self.tableView.dataSource = self
self.tableView.delegate = self
self.tableView.registerClass(ItemCell.self, forCellReuseIdentifier: "cellIdentifier")
并且在你的 cellForRowAtIndexPath
let cell:ItemCell! = tableView.dequeueReusableCellWithIdentifier("cellIdentifier") as? ItemCell