2

I'm trying to create a tableview where each cell has multiple textfields.

I was able to create a simple list with a textfield but when I press the textfield nothing happens. The keyboard does not popup and feels like the event is being captured by some other view.

I made sure that user interaction is set to enable. I've tried to push the view to front like you can see in the comment.

What am I missing?

My table is called MainTable and my InputviewCell is just a normal cell (xib) with a textfield named txtFieldTemp

import Foundation
import UIKit

class PrioritiesGridViewController: UIViewController,UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate
 {

@IBOutlet var mainTable: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    mainTable.delegate = self
    mainTable.dataSource = self
    //mainTable.allowsSelection = false
}

let data = AuxClass()

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 62
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return data.everything().count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = Bundle.main.loadNibNamed("InputTextViewCell", owner: self, options: nil)?.first as! InputTextViewCell

    cell.selectionStyle = UITableViewCellSelectionStyle.none
    cell.txtFieldTemp.delegate = self
    cell.txtFieldTemp.text = "test"

    cell.txtFieldTemp.allowsEditingTextAttributes = true
    //self.view.bringSubview(toFront: cell.txtFieldTemp)

    return cell;
}

override var canBecomeFirstResponder: Bool { return true}

 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    return true
}


func numberOfSections(in tableView: UITableView) -> Int {
    return 1;
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    self.isEditing = true

}


}

class AuxClass
{
var objectsArray = [Objects(
    field1: "teste",
    field2: 3),
    Objects(
        field1: "teste1",
        field2: 4)
]

struct Objects {
    var field1: String!
    var field2: Int
}


func everything() -> [Objects]
{
    return objectsArray
}
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
  • Are you trying to test in simulator mode ? – Jimmy James Mar 14 '17 at 16:26
  • yes on ipad 9.7 landscape ios 10 – João Serra Mar 14 '17 at 16:28
  • Make sure that your keyboard is not disabled in your simulator settings – Jimmy James Mar 14 '17 at 16:39
  • ive tried both suggestions toggle keyboard does not shows the keyboard and trying to type on macs keyboard after the click as selection also doesnt do anything – João Serra Mar 14 '17 at 16:49
  • Also, if you're using NIB for cell, you should register that cell NIB, like http://stackoverflow.com/questions/28489720/uitableviewcell-subclass-with-xib-swift/28490468#28490468 and not load the nib in `cellForRowAt`... – Rob Mar 14 '17 at 16:51
  • sorry rob that was supose to be deleted i was trying to change the "z index" of the views cause i believe the event is being capture in the wrong view for some random reason – João Serra Mar 14 '17 at 16:51
  • Make sure in your View Hierarchy that there is not a view in front of your `UITextView` : (https://www.natashatherobot.com/wp-content/uploads/Screen_Shot_2014-09-28_at_9_31_53_AM-763x1024.png) – Jimmy James Mar 14 '17 at 16:52
  • Jimmy I'm new on swift and xcode, i didn't knew there was such a tool thanks. It seems everything is fine, i don't see any view over the textfield, meaning if i put the mouse over the textfield, the textfield is highlighted with the blue frame – João Serra Mar 14 '17 at 17:04
  • You can rotate that wireframe of the view debugger and you can see if there's anything in front of it... – Rob Mar 14 '17 at 17:07
  • Also, in your NIB (or in the view debugger), make sure you didn't accidentally turn off `userInteractionEnabled` for that text field. You either have something in front of it or its somehow configured to not allow user input... – Rob Mar 14 '17 at 17:10
  • after the whole day wasted i finally figured out. It seems I need to disable "user iteraction enabled" from the view which contains the textfield. The view was capturing the click events. – João Serra Mar 14 '17 at 17:45

1 Answers1

0

Try with cell.txtFieldTemp.becomeFirstResponder(). Looking at the apple documentation I think it will force it to be the first responder.

Jaime Alcántara Arnela
  • 2,062
  • 5
  • 25
  • 56