我对快速编程非常陌生,我正在尝试设置一个程序,该程序会将您从文本框中键入的每个字符添加到TableView中。我已经通过许多论坛和视频进行了故障排除,似乎无法更新我的表视图,当我认为快要关闭时,会引发SIGBART错误。所以我的问题是为什么自定义表格视图单元格不会显示在表格视图中?这是我的代码:
很抱歉,如果这是一个广泛的问题或重复的问题,由于尝试了许多资源,我不知道下一步应该是什么。非常感谢!
import UIKit
//conversion key
let alphaNumToMorse = [
"A": ".-",
"B": "-...",
"C": "-.-.",
"D": "-..",
"E": ".",
"F": "..-.",
"G": "--.",
"H": "....",
"I": "..",
"J": ".---",
"K": "-.-",
"L": ".-..",
"M": "--",
"N": "-.",
"O": "---",
"P": ".--.",
"Q": "--.-",
"R": ".-.",
"S": "...",
"T": "-",
"U": "..-",
"V": "...-",
"W": ".--",
"X": "-..-",
"Y": "-.--",
"Z": "--..",
"a": ".-",
"b": "-...",
"c": "-.-.",
"d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----",
"?":"..--..",
"!":"-.-.--",
" ": " / ",
]
class FirstViewController: UIViewController, UITextViewDelegate, UITableViewDelegate, UITableViewDataSource {
//outlet calls
@IBOutlet weak var translationTextView: UITextView!
@IBOutlet weak var translationLabel: UILabel!
@IBOutlet weak var entertextTextView: UITextView!
@IBOutlet weak var entertextLabel: UILabel!
@IBOutlet weak var clearButton: UIButton!
@IBOutlet weak var copyButton: UIButton!
@IBOutlet weak var sendButton: UIButton!
@IBOutlet weak var hidekeyboardButton: UIButton!
@IBOutlet weak var tableView: UITableView!
//view did load function
override func viewDidLoad() {
super.viewDidLoad()
//sets the entertextTextView and tableView as a UITextView delegate and tableView delegate
self.entertextTextView.delegate = self
tableView.delegate = self
tableView.dataSource = self
//sets translation textview back color and text color
translationTextView.backgroundColor = UIColor.clear
translationTextView.textColor = UIColor.white
//sets translation label forecolor
translationLabel.textColor = UIColor.white
//disables the clear, copy, and send button
clearButton.isEnabled = false
copyButton.isEnabled = false
sendButton.isEnabled = false
hidekeyboardButton.isEnabled = false
}
//adds new rows to the tableview for each character typed
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return entertextTextView.text.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let myText = entertextTextView.text!
cell.textLabel?.text = "\(myText[myText.index(myText.startIndex, offsetBy: indexPath.row)])"
return cell
}
//check if the user has the keyboard visible or not, then enables/disables the hide keyboard button accordingly
func textViewDidBeginEditing(_ textView: UITextView) {
hidekeyboardButton.isEnabled = true
}
func textViewDidEndEditing(_ textView: UITextView) {
hidekeyboardButton.isEnabled = false
}
//when the user touches outside the keyboard, it is no longer shown
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
hidekeyboardButton.isEnabled = false
}
//conversion function
func convertLetterToMorse(_ input: Character) -> String {
var returnChar = alphaNumToMorse[String(input)]
if returnChar == nil {
returnChar = ""
}
return returnChar!
}
var stringToConvert = String()
func convertStringToMorse(_ input: String) -> String {
return input.characters
.compactMap { alphaNumToMorse[String($0)] }
.joined(separator: " ")
}
//button touch up inside functions
//clear button
@IBAction func clearbuttonPress(_ sender: Any) {
entertextTextView.text = ""
translationTextView.text = ""
entertextLabel.isHidden = false
translationLabel.isHidden = false
copyButton.isEnabled = false
sendButton.isEnabled = false
clearButton.isEnabled = false
}
//copy button
@IBAction func copybuttonPress(_ sender: Any) {
UIPasteboard.general.string = translationTextView.text
}
//hide keyboard button
@IBAction func hidekeyboardbuttonPress(_ sender: Any) {
self.view.endEditing(true)
hidekeyboardButton.isEnabled = false
}
//send button
@IBAction func sendbuttonPress(_ sender: Any) {
}
//entertextTextView text change function
func textViewDidChange(_ textView: UITextView) {
//update chars variable everytime textchanges for listview
let chars = Array(entertextTextView.text)
//converted text updated
if entertextTextView != nil {
let outputText = convertStringToMorse(entertextTextView.text)
translationTextView.text = "\(outputText)"
//if entertextTextView letter count is more than zero then . . .
if entertextTextView.text.count > 0 {
//hides entertext and translation labels
entertextLabel.isHidden = true
translationLabel.isHidden = true
//enables clear, copy, and send buttons
clearButton.isEnabled = true
copyButton.isEnabled = true
sendButton.isEnabled = true
hidekeyboardButton.isEnabled = true
}
//if entertextTextView letter count is less than one then . . .
if entertextTextView.text.count < 1 {
//shows entertext and translation labels
entertextLabel.isHidden = false
translationLabel.isHidden = false
//disables clear, copy, and send buttons
clearButton.isEnabled = false
copyButton.isEnabled = false
sendButton.isEnabled = false
}
//Input textview auto adapt font size
if (entertextTextView.text.isEmpty || entertextTextView.bounds.size.equalTo(CGSize.zero)) {
return;
}
let entertextTextViewSize = entertextTextView.frame.size;
let fixedWidth = entertextTextViewSize.width;
let expectSize = entertextTextView.sizeThatFits(CGSize(width : fixedWidth, height : CGFloat(MAXFLOAT)));
var expectFont = entertextTextView.font;
if (expectSize.height > entertextTextViewSize.height) {
while (entertextTextView.sizeThatFits(CGSize(width : fixedWidth, height : CGFloat(MAXFLOAT))).height > entertextTextViewSize.height) {
expectFont = entertextTextView.font!.withSize(entertextTextView.font!.pointSize - 1)
entertextTextView.font = expectFont
}
}
else {
while (entertextTextView.sizeThatFits(CGSize(width : fixedWidth,height : CGFloat(MAXFLOAT))).height < entertextTextViewSize.height) {
expectFont = entertextTextView.font;
entertextTextView.font = entertextTextView.font!.withSize(entertextTextView.font!.pointSize + 1)
}
entertextTextView.font = expectFont;
}
//Translation textview auto adapt font size
if (translationTextView.text.isEmpty || translationTextView.bounds.size.equalTo(CGSize.zero)) {
return;
}
let translationTextViewSize = translationTextView.frame.size;
let fixedWidth2 = translationTextViewSize.width;
let expectSize2 = translationTextView.sizeThatFits(CGSize(width : fixedWidth2, height : CGFloat(MAXFLOAT)));
var expectFont2 = translationTextView.font;
if (expectSize2.height > translationTextViewSize.height) {
while (translationTextView.sizeThatFits(CGSize(width : fixedWidth2, height : CGFloat(MAXFLOAT))).height > translationTextViewSize.height) {
expectFont2 = translationTextView.font!.withSize(translationTextView.font!.pointSize - 1)
translationTextView.font = expectFont2
}
}
else {
while (translationTextView.sizeThatFits(CGSize(width : fixedWidth2,height : CGFloat(MAXFLOAT))).height < translationTextViewSize.height) {
expectFont2 = translationTextView.font;
translationTextView.font = translationTextView.font!.withSize(translationTextView.font!.pointSize + 1)
}
translationTextView.font = expectFont2;
}
}
}
}
答案 0 :(得分:2)
我认为您的代码中有一些问题。首先,看看Apple Tutorial,了解有关动态表视图的一些有用信息。基本上,您需要某种数组来存储从文本视图输入的所有字符串。您可以使用此数组根据索引路径将所有字符串加载到表视图中(这也在Apple教程中)。键入新字符串后,tableView.reloadData()
可用于刷新表视图。
您当前的代码即使正确加载了表格视图,也将仅显示相同文本的许多副本,因为您使用entertextTextView.text
填充了表格中的每个 单元格视图。如前所述,在动态表视图的情况下,遍历字符串数组将是最好的方法。如果您还有其他疑问,请发表评论并提问。祝你好运!
是的,就像提到的其他用户一样,表视图加载代码中有很多小错误,导致崩溃。通过遵循我链接的教程,您应该能够轻松地理解和解决这些问题。
答案 1 :(得分:1)
首先,您不会将dequeueReusableCell()
的结果强制转换为自定义单元格。实际上,您应该通过检查nil
来检查它是否甚至成功出队。
通常,请勿强行打开包装。在guard let
函数中使用if let
或cellForRowAt
来查看哪些变量为零。
您还应该add an exception breakpoint in xcode,当您因无力解开零而崩溃时,它会中断。