Я очень новичок в быстром программировании. У меня есть программа, которую я пытаюсь настроить, которая будет добавлять каждый введенный вами символ из текстового поля в TableView.Я устранял неполадки на многих форумах и в видео, и мне не удается обновить таблицу, и когда я думаю, что приближаюсь, он выдает ошибку SIGBART.Поэтому мой вопрос: почему пользовательские ячейки табличного представления не отображаются в табличном представлении?Вот мой код:
Извините, если это широкий или повторяющийся вопрос, я не знал, каким должен быть мой следующий шаг, поскольку я перепробовал много ресурсов.Большое вам спасибо!
![StoryBoard View](https://i.stack.imgur.com/2Aj46.png)
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;
}
}
}
}