2016-06-10 49 views
0

我有一个自定义的几个网点tableviewcell如何从tableviewcell到tableview的出口数据?

class ShopTableViewCell: UITableViewCell { 

    @IBOutlet var orderName: UITextField! 

    @IBOutlet var specialinstructions: UITextField! 
    @IBOutlet var shopName: UITextField! 


} 

我有一个的tableView是

class ConvenienceTableViewController: UITableViewController, UITextFieldDelegate { 

     override func viewDidLoad() { 
     super.viewDidLoad() 

     } 

     func barTapped() { 
     // I want to do some validation here, and getting the outlet's data from tableviewcell 
     } 

} 

,我能想到拿到出口的数据的唯一方法是使用cellForRowAtIndexPath这是

cell.orderName 
cell.specialinstructions 
cell.shopName 

但是,如何获得这些插座的数据并将其放入func barTapped

+0

何时调用barTapped? – Paulw11

+0

何时调用barTapped()?它是否选择了UITableViewCell?它是每个表格视图单元格中的按钮吗? – user3179636

回答

2
let currentCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: requiredRow, inSection: requiredSection)) as? ShopTableViewCell 
let orderName = currentCell?.orderName 

使用此在您的barTapped

0

假设barTapped是分配给该按钮的动作宣告正确传递按钮参数的方法。由于表视图单元通常是按钮调用superview的超级视图并获取索引路径。然后,您可以直接从模型(数据源数组)获取数据,而不是从视图(单元格)中获取数据。

func barTapped(button : UIButton) { 
    let cell = button.superview as! ShopTableViewCell 
    let indexPath = tableView.indexPathForCell(cell) 

} 
0

正确的方法是使用DataSource中的数据,这是您为您的单元格提供的数据!

对于答案的第二部分,我假设你可以点击你的手机吧。

如果你想你说的办,那么也许使用委托:

protocol ShopTableViewCellDelegate { 
func barTapped(cellFromDelegateMethod: ShopTableViewCell) 
} 

可以称之为细胞是这样的:

@IBAction func someBarTapped(sender: AnyObject) { 
    delegate?.barTapped(self) 
} 

然后

class ConvenienceTableViewController: UITableViewController, UITextFieldDelegate, ShopTableViewCellDelegate { 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    } 

    func barTapped(cellFromDelegateMethod: ShopTableViewCell) { 
     //here you have your cell 
    } 

} 

(记得在你的ViewController的某个位置设置这个单元格的代表!)

相关问题