我在UITableView节标题中出现UILabel(变量名称:wrongCountLabel)未对齐的情况,因为它以编程方式设置为固定的x和y坐标。这些坐标适用于较小的屏幕,但在较大的屏幕上却缺少右侧(请参见下面的屏幕转储)。
自从我在代码中创建了节标题以来,我试图以编程方式将“ Wrong(times)”标签的后沿锚定到UITableView的后沿。当我运行该小部件时,它说它无法加载数据。
//
// TodayViewController.swift
// Widget
//
// Created by on 10/02/2019.
// Copyright © 2019. All rights reserved.
//
import UIKit
import NotificationCenter
class TodayViewController: UIViewController, NCWidgetProviding, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var words = [String]()
var sortedPracticeWords = [String]()
var chosenLanguage = String()
let wordsString = "Words"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view from its nib.
if let defaults = UserDefaults(suiteName: "group.co.uk.tirnaelectronics.polyglot") {
if let savedLanguage = defaults.object(forKey: "languageChosen") as? String {
print("savedLanguage is: \(savedLanguage)")
chosenLanguage = savedLanguage
if let savedWords = defaults.object(forKey: "\(chosenLanguage)\(wordsString)") as? [String] {
words = savedWords
}
}
}
extensionContext?.widgetLargestAvailableDisplayMode = .expanded
sortPracticeWords()
}
func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize) {
if activeDisplayMode == .compact {
preferredContentSize = CGSize(width: 0, height: 110)
} else {
preferredContentSize = CGSize(width: 0, height: 440)
}
}
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
// Perform any setup necessary in order to update the view.
// If an error is encountered, use NCUpdateResult.Failed
// If there's no update required, use NCUpdateResult.NoData
// If there's an update, use NCUpdateResult.NewData
completionHandler(NCUpdateResult.newData)
}
func sortPracticeWords() {
var practiceWords = [String]()
for i in 1..<words.count {
if Int(words[i - 1].components(separatedBy: "::")[2]) ?? 0 > 0 {
practiceWords.append(words[i - 1])
}
}
var sortedAboveIndex = practiceWords.count
var swaps = 0
var tempPracticeWord = String()
repeat {
var lastSwapIndex = 0
for i in 1..<sortedAboveIndex {
if Int(practiceWords[i - 1].components(separatedBy: "::")[2])! < Int(practiceWords[i].components(separatedBy: "::")[2])! {
tempPracticeWord = practiceWords[i]
practiceWords[i] = practiceWords[i - 1]
practiceWords[i - 1] = tempPracticeWord
lastSwapIndex = i
swaps += 1
}
}
sortedAboveIndex = lastSwapIndex
} while (sortedAboveIndex != 0)
sortedPracticeWords = practiceWords
print("sortedPracticeWords are: \(sortedPracticeWords)")
print("practiceWords is sorted in \(swaps) swaps.")
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sortedPracticeWords.count
}
internal func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = UIColor(white: 1, alpha: 0.2)
let languageLabel = UILabel()
languageLabel.text = "\(chosenLanguage.capitalized)"
languageLabel.frame = CGRect(x: 10, y: 5, width: 170, height: 20)
//languageLabel.leadingAnchor.constraint(equalTo: tableView.layoutMarginsGuide.leadingAnchor).isActive = true
view.addSubview(languageLabel)
let wrongCountLabel = UILabel()
wrongCountLabel.text = "Wrong (times)"
wrongCountLabel.textAlignment = .left
wrongCountLabel.frame = CGRect(x: 180, y: 5, width: 120, height: 20)
//wrongCountLabel.trailingAnchor.constraint(equalTo: tableView.layoutMarginsGuide.trailingAnchor).isActive = true
view.addSubview(wrongCountLabel)
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:TodayCustomCell = self.tableView.dequeueReusableCell(withIdentifier: "PracticeWord") as! TodayCustomCell
let sortedPracticeWord = sortedPracticeWords[indexPath.row]
print("practiceWord is: \(sortedPracticeWord)")
let split = sortedPracticeWord.components(separatedBy: "::")
cell.practiceWord.textColor = UIColor(white: 1, alpha: 0.75)
cell.selectedBackgroundView = UIView()
cell.selectedBackgroundView!.backgroundColor = UIColor(white: 1, alpha: 0.20)
cell.practiceWord.frame = CGRect(origin: CGPoint(x: 10, y: 10), size: CGSize(width: 200, height: 40))
cell.wrongCount.frame = CGRect(origin: CGPoint(x: 210, y: 10), size: CGSize(width: 100, height: 40))
cell.practiceWord?.text = split[1]
cell.wrongCount?.text = split[2]
print("cell is: \(cell)")
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
/*if let cell = tableView.cellForRow(at: indexPath) {
let practiceWord = sortedPracticeWords[indexPath.row]
let split = practiceWord.components(separatedBy: "::")
cell.detailTextLabel?.text = split[2]
print("Detail cell is: \(cell)")
}*/
}
}
右手屏幕应该像左手屏幕一样显示,只是在更大的屏幕上显示。
我需要一些锚定UILabel的代码,以便无论用户的屏幕大小如何,它们都能工作。
答案 0 :(得分:0)
会改变:
wrongCountLabel.textAlignment = .left
收件人
wrongCountLabel.textAlignment = .right
帮助?