ios - 自定义 TableView 单元格未添加到 tableview

标签 ios swift uitableview

我是快速编程的新手 我有一个程序,我正在尝试设置它会将您从文本框中键入的每个字符添加到 TableView 中。我已经通过许多论坛和视频进行了故障排除,但似乎无法让我的 tableview 更新,当我认为我接近时,它会抛出 SIGBART 错误。所以我的问题是为什么自定义 tableview 单元格不会出现在 tableview 中?这是我的代码:

抱歉,如果这是一个广泛或重复的问题,我不知道我的下一步应该是什么,因为我已经尝试了很多资源。非常感谢!

StoryBoard View

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;
        }
  } 
}
}

最佳答案

我认为您的代码中存在一些问题。首先,看一下 Apple Tutorial有关动态表格 View 的一些有用信息。基本上,您需要某种数组来存储从 TextView 输入的所有字符串。根据索引路径(这也在 Apple 教程中),您使用此数组将所有字符串加载到 TableView 中。 tableView.reloadData() 可用于在输入新字符串后刷新 TableView 。

您当前的代码,即使它能够正确加载表格 View ,也只会显示相同文本的多个副本,因为您使用 entertextTextView.text 来填充每个 表格 View 中的单元格。就像我之前提到的,遍历字符串数组将是动态 TableView 的最佳方式。如果您还有其他问题,请发表评论并提问。祝你好运!

是的,就像其他用户提到的那样,您的 TableView 加载代码中存在许多小错误,导致它崩溃。按照我链接的教程,您应该能够轻松理解和解决这些问题。

关于ios - 自定义 TableView 单元格未添加到 tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52845151/

相关文章:

IOS swift tableview获取一个部分的headerview

ios - 我怎样才能让 tableViewController 知道 managedObjectContext 在外面发生了变化?

ios - 波形颜色变化的动画图像删除

ios - UITable 滚动不流畅,单元格在 IOS 中显示错误信息

ios - 动态TableView链接到文本

swift - Swift 中的底部类型

ios - 如何在新搜索中取消 uitableviewcells 的异步图像加载

ios - 带有协议(protocol)的自定义 Cell,用于检查 UISwitch 中的更改

html - 在 iOS 上显示 HTML 内容和 native 内容的最佳解决方案是什么?

swift - 如何从 firebase 读取数据