用户输入无法在“表格视图”中以编程方式添加文本字段

时间:2018-02-13 03:58:24

标签: ios swift uitableview uitextfield

我正在尝试添加用户可编辑的文本字段。我能够为表格的每个不同单元格显示文本字段,但我无法让用户输入。它不显示键盘。我启用了用户交互。这是我的代码。请忽略已注释掉的部分。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "transportCell", for: indexPath)

  //self.tableView.rowHeight = 100



    //var a = Array(words[indexPath.row])

    //a.shuffle()

    //var shuffledWord = String(a)


    //shuffledWord = shuffledWord.lowercased()

    let sampleTextField =  UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
    sampleTextField.placeholder = "Enter text here"
    sampleTextField.font = UIFont.systemFont(ofSize: 15)
    sampleTextField.borderStyle = UITextBorderStyle.roundedRect
    sampleTextField.autocorrectionType = UITextAutocorrectionType.no
    sampleTextField.keyboardType = UIKeyboardType.alphabet
    sampleTextField.returnKeyType = UIReturnKeyType.done
    sampleTextField.clearButtonMode = UITextFieldViewMode.whileEditing;
    sampleTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.center
    sampleTextField.delegate = self as? UITextFieldDelegate


   // cell.textLabel?.text = shuffledWord

   // var imageName = UIImage(named: words[indexPath.row])
    //cell.imageView?.image = imageName

    return cell
}

如果可能,您还可以告诉我如何将用户的输入存储为变量吗?

提前致谢

2 个答案:

答案 0 :(得分:2)

正如@rmaddy所说,首先,您必须将该文本字段添加到您的单元格中...

HTTP Error 400: Bad Request
Error for URL https://graph.facebook.com/v2.12/164637176974573_1282363268535286/likes/?limit=2000&access_token=XXXXX|XXXXX&after=MTk5MzAxOTI5MDk3MTQyNwZDZD/Page-id/likes?limit=5000&access_token= XXXXX|XXXXX: 2018-02-12 15:53:18.585000

第二,您可以通过管理像...

这样的字符串数组来存储用户值
def nth_prime_number(n):
    # initial prime number list
    prime_list = [2]
    # first number to test if prime
    num = 3
    # keep generating primes until we get to the nth one
    while len(prime_list) < n:

        # check if num is divisible by any prime before it
        for p in prime_list:
            # if there is no remainder dividing the number
            # then the number is not a prime
            if num % p == 0:
                # break to stop testing more numbers, we know it's not a prime
                break

        # if it is a prime, then add it to the list
        # after a for loop, else runs if the "break" command has not been given
        else:
            # append to prime list
            prime_list.append(num)

        # same optimization you had, don't check even numbers
        num += 2

    # return the last prime number generated
    return prime_list[-1]

x = nth_prime_number(8)
print(x)

并从文本字段委托中获取值,如此...

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {......

  sampleTextField.text = arrTextFieldVal[indexPath.item]//this array for stroing the user input values..
  sampleTextField.tag = indexPath.item // u will access the text field with this value 
  cell.addSubview(sampleTextField)........}

答案 1 :(得分:0)

请参阅this answer,看看它是否对您有帮助。

  • 另外,请检查您是否在textFieldShouldBeginEditing,shouldChangeCharactersIn或textFieldShouldBeginEditing textates的委托中退出键盘。