如何在@IBAction按钮中为行委托方法tableview swift 3获取单元格中的数据

时间:2017-03-01 10:49:53

标签: ios uitableview swift3

var textfield_name:String? 

@IBAction func Btn_submit(_ sender: Any){
    let position: CGPoint = (sender as AnyObject).convert(CGPoint.zero, to: table_view)
    if let indexPath = table_view.indexPathForRow(at: position){
        let Section_count = table_view.numberOfSections
        let rowsCount = table_view.numberOfRows(inSection: 0)
        for i in 0..<rowsCount{
            // let index = IndexPath(row: i, section: 0)
            let cell = table_view.cellForRow(at: indexPath) as! UITableViewCell
            // let cell: UITableViewCell = table_view.cellForRow(at: index) as! UITableViewCell
            textfield_name = cell.text_field.text!
            print("cell is \(cell)")
        }
    }
}

我在表格视图中有多个部分。在章节中,我有两个文本字段和一些(Radio)按钮。我在最后一个单元格中有提交按钮。我想从文本字段和选定按钮获取提交按钮@IBAction的数据。

2 个答案:

答案 0 :(得分:0)

最常见的方法是在文本字段结束编辑时收集数据。 UITextFieldDelegate的委托方法textFieldDidEndEditing。

答案 1 :(得分:0)

您可以从以下位置跟踪UIButton操作:

func getDataForButton(sender: UIButton!){
        let buttonPosition = sender.convert(sender.bounds.origin, to: tblView)
        let btnIndexPath = tblView.indexPathForRow(at: buttonPosition)

        // reload the particular row if needed or fetch the data
        tblView.reloadRows(at: [btnIndexPath!], with: .none)

    }