Search code examples
iosswiftuitableviewcell

Custom Cell TextView Repeat


I have been stuck for hours on this issue. I have created a custom cell inside a tableview which is inside a UIVC. The custom cell has it's own class file and is all linked up as well as having the buttons inside it connected to the UIVC via delegates.

Inside the custom cell there consists of a UITextView as well as some buttons.

Tapping the tableview adds a new custom cell. The user can type whatever their beautiful heart desires, eventually resigning the first responder of the textview. Here is my issue(s).

MAIN. First off. Overtime, the cells start mixing up the text that was typed in by the user, and starts reusing it. It becomes a mess.

Due to the fact I have a custom cell, I do not need to register the class in the viewDidLoad of the UIVC. A stack overflow answer stated.

Optional. Second. Eventually, after a certain point the keyboard blocks the cell view. I have no idea how to scroll the cell to the top of the tableview.

Optional. Third. This is just a bonus. How would one be able to save the data from a cell with a decent storage method. I heard NSUserDefaults is only good for small memory storage files. Basically. Transferring the data in the textview inside the custom cell, into another area (UIVC?) where the data can be saved. Most likely using an array and saving the data on that array.

Code. As requested.

UIVC

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


        let reminderCell = reminder_tableView.dequeueReusableCellWithIdentifier("cellReminder", forIndexPath:indexPath) as! addReminderCell

        reminderCell.selectionStyle = .None
        reminderCell.heightForReminderTextDelegate = self
        reminderCell.delegate = self

        reminderCell.reminder_textview.becomeFirstResponder()

        return reminderCell


    }

Custom Cell Code. Not all of it though.

import UIKit
import AVFoundation




//MARK: Height For Reminder Delegate
protocol HeightForReminderTextView
{
    func heightOfTextView(height: CGFloat)


}


//MARK: Beginning of Class

class addReminderCell: UITableViewCell, UITextViewDelegate {





    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: nil)




    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)



    }



    //MARK: Delegate Declarations

    var heightForReminderTextDelegate :HeightForReminderTextView?




    //MARK: Element IBOutlets

    @IBOutlet var reminder_textview: UITextView!
    @IBOutlet var reminder_cell_uiview: UIView!


    //MARK: Button
    @IBAction func non_priority_button(sender: AnyObject) {

        println("pressed")

    }


  //MARK: Awake From Nib Starts Here

    override func awakeFromNib() {
        super.awakeFromNib()

        reminder_textview.delegate = self

          }

Solution

  • MAIN: When you dequeue a cell it means that cell may be reused it does not create a new cell everytime. For that UITableViewCell has a method named prepareForReuse() which you can override, where you can reset all your cell content. Ex:

    override func prepareForReuse() {
        myTextView.text = ""
    }
    

    Due to the fact I have a custom cell...
    If you have only *.swift file for your custom cell you must registerClass(:forCellReuseIdentifier:)
    If you also have *.xib file you should registerNib(
    :forCellReuseIdentifier:) in the viewDidLoad

    Optional
    You can use scrollToRowAtIndexPath(_:atScrollPosition:animated:) to scroll desired cell to top of tableView

    Optional
    You can use CoreData, JSON, some SQLite wrappers to save data. In NSUserDefaults you can use small amount of data, like some settings. I recommend to use NSJSONSerialization for now. You can create NSData from a Dictionary and write data as *.json to Documents directory using NSFileManager. In your app you should keep an array or dictionary with all the data and pass to another view controller that data.