0

I am very new to swift programming I have a program that I am trying to set up that will add each character that you type from a textbox into TableView. I have troubleshooted through many forums and videos and can't seem to get my tableview to update, and when I think I am getting close it throws a SIGBART error. So my question is why won't the custom tableview cells show up in the tableview? Here is my code:

Sorry if this is a broad or repeated question, I didn't know what my next step should be as I have tried many resources. Thank you so much!

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;
        }
  } 
}
}
  • 2
    As far as your code you posted here, i see that in your `cellForRow:` you are using native tableViewCell and NOT your customCell. You need to cast tableViewCell to your customCell. – Rumin Oct 16 '18 at 23:25

2 Answers2

2

I think that you have a few problems in your code. First of all, take a look at the Apple Tutorial for some good information on dynamic tableviews. Basically, you need some kind of array to store all of the strings that are inputed from the text view. You use this array to load all of the strings onto the table view, according to the index path(this is also in the Apple Tutorial). tableView.reloadData() can be used to refresh the table view after a new string is typed in.

Your current code, even if it were to correctly load the table view, would only display many copies of the same text, because you use entertextTextView.text to populate every cell in the table view. Like I mentioned before, iterating through an array of strings will be the best way in your case of a dynamic table view. If you have any further questions, please comment and ask. Good luck!

And yes, like other users mentioned, there are many small errors in your table view loading code, causing it to crash. By following the tutorial I linked, you should be able to understand and solve these issues easily.

Xcoder
  • 1,433
  • 3
  • 17
  • 37
1

To start, you're not casting the result of dequeueReusableCell() to your custom cell. In fact, you should check to see if it even dequeues successfully by checking for nil.

In general, don't force unwrap. Use guard let or if let in your cellForRowAt function to see which variables are nil.

You should also add an exception breakpoint in xcode, which will break any time you crash on something like a force unwrap of nil.

Kon
  • 4,023
  • 4
  • 24
  • 38