Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在点击按钮时将Tableview单元格内的文本字段中的数据打印到控制台_Ios_Swift_Uitextfield_Tableview - Fatal编程技术网

Ios 如何在点击按钮时将Tableview单元格内的文本字段中的数据打印到控制台

Ios 如何在点击按钮时将Tableview单元格内的文本字段中的数据打印到控制台,ios,swift,uitextfield,tableview,Ios,Swift,Uitextfield,Tableview,我想在单击tableview外的按钮时,将tableview单元格内的textfield中的数据打印到控制台。 当我使用按钮的iAction方法打印数据时,我只获得单行数据。 这是我的更新代码供参考 //Deliverview cell import UIKit //Protocol protocol Delivery { func OnClick(index : Int) } class DeliveryViewCell: UITableViewCell { @IBOutle

我想在单击tableview外的按钮时,将tableview单元格内的textfield中的数据打印到控制台。 当我使用按钮的iAction方法打印数据时,我只获得单行数据。 这是我的更新代码供参考

//Deliverview cell
import UIKit
//Protocol
protocol  Delivery {
    func OnClick(index : Int)
}
class DeliveryViewCell: UITableViewCell {
    @IBOutlet weak var cellTextField: UITextField!
    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var cellImageView: UIImageView!
    var celldelegate : Delivery?
    var index : IndexPath?
    var data: DeliveryData? {
        didSet {
            updateCell()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        cellTextField.borderStyle = .none
        cellTextField.delegate = self
        // Initialization code
    }
    func updateCell() {
        if let data = data {
            cellTextField.text = data.value
            cellTextField.placeholder = data.placeholder
        }
    }

    @IBAction func pickLocation(_ sender: Any) {
        celldelegate?.OnClick(index: index!.row)
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension DeliveryViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ cellTextField: UITextField) {
        deliveryData1.value = cellTextField.text!
    }
}
//View controller
class DeliveryData {
    let placeholder: String
    var value: String = ""
    
    init(_ placeholder: String) {
        self.placeholder = placeholder
    }
}

var deliveryData1: [DeliveryData] = [
    DeliveryData("Name"),
    DeliveryData("Phone"),
    DeliveryData("Email"),
    DeliveryData("order"),
    DeliveryData("Address"),
    DeliveryData("Date"),
    DeliveryData("Description"),
    DeliveryData("Barcode"),
    DeliveryData("Image")
]

从您描述问题的方式来看,我相信您正在尝试打印与特定单元格关联的文本字段的值。你有几种方法来实现这一点

方法1(最简单) 在这个场景中,对于初学者来说最简单的方法如下。 更改cellForRowAt以执行以下操作:

let cell=deliveryTableView.dequeueReusableCell(带有标识符:“cell”,for:indexath)as!DeliveryViewCell
cell.cellTextField.placeholder=deliveryData[indexPath.row]
//按钮是指您拥有或应该创建的按钮出口。
cell.button.tag=indexPath.row
返回单元
然后将按钮的操作更改为:

@IBAction func tapSaveData(uu发送方:ui按钮){
让row=sender.tag//在单元格出列时保存的标记
让cell1=deliveryTableView.cellForRow(位于:IndexPath.init(行:行,节:0))作为!DeliveryViewCell
打印(cell1.cellTextField.text!)
}
方法2 您可以简单地在单元格内使用闭包

假设这是自定义单元格的一部分:

。。。你的代码
变量onTextFieldEndEditing:((字符串?->无效)?
func textField编辑(textField:UITextField){
//编辑完成后。
onTextFieldEndEditing?(textField.text)
}
然后,在你的牢房里:

let cell=deliveryTableView.dequeueReusableCell(带有标识符:“cell”,for:indexath)as!DeliveryViewCell
cell.cellTextField.placeholder=deliveryData[indexPath.row]
cell.onTextFieldEndEditing={[weak self]中的文本
打印(文本)
}
返回单元
在方法2中,按钮不再需要@iAction或按钮本身。 如果您仍然希望调用print on按钮点击,只需在单元格内部创建一个@iAction,然后在点击按钮时调用闭包,而不是在文本字段结束编辑时调用闭包。
如果您还有任何问题,请告诉我。

从您描述问题的方式来看,我相信您正在尝试打印与特定单元格关联的文本字段的值。你有几种方法来实现这一点

方法1(最简单) 在这个场景中,对于初学者来说最简单的方法如下。 更改cellForRowAt以执行以下操作:

let cell=deliveryTableView.dequeueReusableCell(带有标识符:“cell”,for:indexath)as!DeliveryViewCell
cell.cellTextField.placeholder=deliveryData[indexPath.row]
//按钮是指您拥有或应该创建的按钮出口。
cell.button.tag=indexPath.row
返回单元
然后将按钮的操作更改为:

@IBAction func tapSaveData(uu发送方:ui按钮){
让row=sender.tag//在单元格出列时保存的标记
让cell1=deliveryTableView.cellForRow(位于:IndexPath.init(行:行,节:0))作为!DeliveryViewCell
打印(cell1.cellTextField.text!)
}
方法2 您可以简单地在单元格内使用闭包

假设这是自定义单元格的一部分:

。。。你的代码
变量onTextFieldEndEditing:((字符串?->无效)?
func textField编辑(textField:UITextField){
//编辑完成后。
onTextFieldEndEditing?(textField.text)
}
然后,在你的牢房里:

let cell=deliveryTableView.dequeueReusableCell(带有标识符:“cell”,for:indexath)as!DeliveryViewCell
cell.cellTextField.placeholder=deliveryData[indexPath.row]
cell.onTextFieldEndEditing={[weak self]中的文本
打印(文本)
}
返回单元
在方法2中,按钮不再需要@iAction或按钮本身。 如果您仍然希望调用print on按钮点击,只需在单元格内部创建一个@iAction,然后在点击按钮时调用闭包,而不是在文本字段结束编辑时调用闭包。
如果您还有任何问题,请告诉我。

UITableViewCell
s不用于存储状态。滚动时,单元格将被重新使用,并且它们可能具有的任何值(即用户输入的文本)都将丢失

解决方案是保存用户对数据模型所做的任何更改。这可以是类、数组、字典等。在您的情况下,
DeliveryViewCell
需要符合
UITextFieldDelegate
,以便在用户完成编辑时收到通知:

extension DeliveryViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        //Do something here to save the text...
    }
}
然后,只需将更新后的文本存储在某个地方。这里有一个选择,但决不是唯一或最好的方法:

//In your cell
var data: DeliveryData? {
    didSet {
        updateCell()
    }
}

override func awakeFromNib() {
    super.awakeFromNib()

    textField.delegate = self
}

func updateCell() {
    if let data = data {
        textField.text = data.value
        textField.placeholder = data.placeholder
    }
}

extension DeliveryViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        deliveryData?.value = textField.text!
    }
}

//In your view controller
class DeliveryData {
    let placeholder: String
    var value: String = ""

    init(_ placeholder: String) {
        self.placeholder = placeholder
    }
}
var deliveryData: [DeliveryData] = [
    DeliveryData("Name"),
    DeliveryData("Phone"),
    DeliveryData("Email"),
    DeliveryData("order"),
    DeliveryData("Address"),
    DeliveryData("Date"),
    DeliveryData("Description"),
    DeliveryData("Barcode"),
    DeliveryData("Image")
]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = deliveryTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DeliveryViewCell

    cell.data = deliveryData[indexPath.row]

    return cell
}

@IBAction func tapSaveData(_ sender: Any) {
    for data in deliveryData {
        print(data.value)
    }
}

这还有一个额外的好处,即当单元格被卷回视图时,恢复用户键入的任何文本。

UITableViewCell
s并不用于存储状态。滚动时,单元格将被重新使用,并且它们可能具有的任何值(即用户输入的文本)都将丢失

解决方案是保存用户对数据模型所做的任何更改。这可以是类、数组、字典等。在您的情况下,
DeliveryViewCell
需要符合
UITextFieldDelegate
,以便在用户完成编辑时收到通知:

extension DeliveryViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        //Do something here to save the text...
    }
}
然后,只需将更新后的文本存储在某个地方。这里有一个选择,但决不是唯一或最好的方法:

//In your cell
var data: DeliveryData? {
    didSet {
        updateCell()
    }
}

override func awakeFromNib() {
    super.awakeFromNib()

    textField.delegate = self
}

func updateCell() {
    if let data = data {
        textField.text = data.value
        textField.placeholder = data.placeholder
    }
}

extension DeliveryViewCell: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        deliveryData?.value = textField.text!
    }
}

//In your view controller
class DeliveryData {
    let placeholder: String
    var value: String = ""

    init(_ placeholder: String) {
        self.placeholder = placeholder
    }
}
var deliveryData: [DeliveryData] = [
    DeliveryData("Name"),
    DeliveryData("Phone"),
    DeliveryData("Email"),
    DeliveryData("order"),
    DeliveryData("Address"),
    DeliveryData("Date"),
    DeliveryData("Description"),
    DeliveryData("Barcode"),
    DeliveryData("Image")
]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = deliveryTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! DeliveryViewCell

    cell.data = deliveryData[indexPath.row]

    return cell
}

@IBAction func tapSaveData(_ sender: Any) {
    for data in deliveryData {
        print(data.value)
    }
}

这还有一个额外的好处,即当单元格被卷回视图时,恢复用户键入的任何文本。

您正在打印cell1数据吗?您需要什么/是的,我正在使用cell1打印tableview中所有行的数据。您可以打印所有可见单元格文本字段值。。。或者您需要将它们存储为TableView单元格reused@satyamkumar-请参阅此答案:是否正在打印cell1数据?您需要什么/是的,我正在使用cell1进行pri