1

我正在尝试格式化 UIViewController 的 detailTextLabel 属性中的数字位置。UIViewTable 的 detailTextLabel 部分中的数字太靠右了(如图所示)。

对齐问题截图

我试过了:

cell.detailTextLabel?.textAlignment = .center

但它不起作用。我在界面生成器中尝试了.left.right和各种设置。.justified

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PracticeWord", for: indexPath)

        let sortedPracticeWord = sortedPracticeWords[indexPath.row]
        print("practiceWord is: \(sortedPracticeWord)")

        let split = sortedPracticeWord.components(separatedBy: "::")

        cell.textLabel?.text = split[0]

        cell.textLabel?.textColor = UIColor.white

        cell.selectedBackgroundView = UIView()
        cell.selectedBackgroundView!.backgroundColor = UIColor(white: 1, alpha: 0.20)

        cell.textLabel?.text = split[1]
        cell.detailTextLabel?.text = split[2]
        cell.detailTextLabel?.textAlignment = .center

        print("cell is: \(cell)")
        return cell
    }

我希望每个数字都在“错误”一词的“g”下结束。

4

3 回答 3

0

按照本教程创建自定义单元格后:

快速创建自定义表格视图单元格

它似乎给了我最初想要的东西。

创建自定义单元格后的新对齐方式

于 2019-04-03T20:14:47.080 回答
0

详细文本标签仅使用内置的 UITableViewCell 样式显示,并且不会尊重对齐方式,因为默认样式会做自己的事情并且不会给您太多控制。这对于简单的东西来说很好,但很快就会成为任何不平凡的事情的限制。

如果您想控制放置,您需要使用左右 UILabel 定义自己的自定义表格单元格,并将它们精确地限制在您想要的位置。另请记住,如果用户更改系统字体大小,您可能仍然无法与“g”字符对齐,因此您可能需要考虑不同的设计,或者只是不担心对齐。

有关内置样式的描述,请参阅https://developer.apple.com/documentation/uikit/uitableviewcell/cellstyle,但我怀疑如果默认样式没有达到您想要的效果,您需要创建自己的样式。

于 2019-04-03T18:29:14.507 回答
0

我认为这里发生的是 detailTextLabel 正在调整大小以适合文本长度,并且整个标签与右边缘对齐。

我会尝试在添加到详细文本标签的文本中添加一个空格。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "PracticeWord", for: indexPath)

    let sortedPracticeWord = sortedPracticeWords[indexPath.row]
    print("practiceWord is: \(sortedPracticeWord)")

    let split = sortedPracticeWord.components(separatedBy: "::")

    cell.textLabel?.text = split[0]

    cell.textLabel?.textColor = UIColor.white

    cell.selectedBackgroundView = UIView()
    cell.selectedBackgroundView!.backgroundColor = UIColor(white: 1, alpha: 0.20)

    cell.textLabel?.text = split[1]
    cell.detailTextLabel?.text = split[2] + "   "
    cell.detailTextLabel?.textAlignment = .center

    print("cell is: \(cell)")
    return cell
}
于 2019-04-03T16:20:51.973 回答