1

I want to insert in textfields in specific cells of the tableview. I have an array of 12 strings (3 of which are empty spaces). Those blank cells in the tableview, I want to create a text field in them so user can type in those empty slots. But I am having trouble creating text fields only in those slots which are blank. How should I do that?

    var items = ["Apple", "Fish", "Dates", "Cereal", "Ice cream", "Lamb", "Potatoes", "Chicken", "Bread", " ", " "," "]

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cellFruit", forIndexPath: indexPath)

    if (items[indexPath.row] == " ") {
        cell.textLabel?.text = items[indexPath.row]
        let collar = UIColor.init(red: 175, green: 189, blue: 212, alpha: 0.4)
        cell.backgroundColor = collar
        return cell

    }
    else {
        cell.textLabel?.text = items[indexPath.row]
        let coL = UIColor.init(red: 59, green: 89, blue: 152, alpha: 0.2)
        cell.backgroundColor = coL
        return cell

    }

}

here is how i reload the cells. now the text field is showing but it begins to show in cells where string is non-empty spaces.

     func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
    if (sourceIndexPath.row != destinationIndexPath.row){
        let temp = items[sourceIndexPath.row]
        items.removeAtIndex(sourceIndexPath.row)
        items.insert(temp, atIndex: destinationIndexPath.row)

    }
    tableView.reloadData()
}
Osaama Shehzad
  • 147
  • 1
  • 2
  • 12

2 Answers2

2

You may create two cells (or one cell used init(style: UITableViewCellStyle, reuseIdentifier: String?)) with cell identifiers "default" and "wTextfield" for example. One cell default, second cell will have text field. Register them in viewDidLoad if they are not from storyboard (no needed if they from storyboard):

override func viewDidLoad()
{        
    super.viewDidLoad()
    //some setup

    tableView.registerClass(MyCell, forCellReuseIdentifier: "default")
    tableView.registerClass(MyCell, forCellReuseIdentifier: "wTextfield")
}

Then, when you setting up your cells in cellForRowAtIndexPath method, you may choose right cell for you:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cellId = "default"
    if items[indexPath.row] == " " {cellId = "wTextfield"}
    let cell = tableView.dequeueReusableCellWithIdentifier(cellId, forIndexPath: indexPath) as! MyCell

    //set up your cell now
    return cell
}
Yury
  • 6,044
  • 3
  • 19
  • 41
  • I was able to create text fields successfully. But now there is another problem. every time I scroll through the cells, the ones with text field look good but text field also begins to show on cells where it's not supposed to. I have posted my code where reloading of tableview is done. can you guide me where i am wrong? – Osaama Shehzad Jun 28 '16 at 09:13
  • @OsaamaShehzad did you implement my solution or solution from deleted answer? – Yury Jun 28 '16 at 09:21
  • i implemented the solution from the deleted answer. i think something is wrong with that :/ in your solution, do i implement the text field the same way as I did from last answer – Osaama Shehzad Jun 28 '16 at 09:25
  • @OsaamaShehzad in my solution you should separate cases when you have text field and when you don't. If you using storyboard (I believe), you should make TWO prototype cell's in your table view, and set they `cellIdentifier`'s properly as in my answer. Of course, you should create custom `UITableViewCell` class for this cells (can be one for both; I assumed name of it `MyCell`). There are many many tutorials about `UITableView` and how you can add prototype cell's in it, search for them if you have problems at this step. After that you can use `cellForRowAtIndexPath` template from my answer – Yury Jun 28 '16 at 09:33
  • I just made another prototype cell and did as you suggested. It's working better now. Except the fact that it is overwriting. It is not allowing me to erase and rewrite in the cells. How do I fix that? – Osaama Shehzad Jun 28 '16 at 09:47
  • @OsaamaShehzad I'm not sure what are you talking about. Probably about that entered text disappears after table view moved. You should update your data model (`items` in your case) after every text change. You should use delegate methods of `UITextField` for listening events from text fields. – Yury Jun 28 '16 at 10:03
  • actually, no. whenever i type, if i want to type again, it doesn't remove the old text. instead, it writes over the previous text in the cell. how can I have the option to delete what i wrote and write again? – Osaama Shehzad Jun 28 '16 at 10:19
  • also, how do I use the delegate methods when the textfield is part of the table view cell? I am new to Swift. 2 weeks only that's why I am confused. – Osaama Shehzad Jun 28 '16 at 10:27
  • @OsaamaShehzad read more tutorials, feel free to google. This your questions answered many times. Try to read this answer about delegate http://stackoverflow.com/questions/4375442/accessing-uitextfield-in-a-custom-uitableviewcell – Yury Jun 28 '16 at 10:37
0

You should check your string values in your tableView:cellForRowAtIndexPath function and show/hide text field depending on your string value.

Sunny
  • 137
  • 7
  • how do i create a text field in the first place before deciding whether to show it or not? I can't figure out the code to do so. :/ – Osaama Shehzad Jun 28 '16 at 08:56