2016-06-10 25 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的某個位置設置這個單元格的代表!)

相關問題