這裏有一個簡單的解決方案。它可以用於任何數量的單元。
你需要做的是通過迭代細胞,並計算出,如果特定的細胞是抱着文本框爲空。現在的問題是你將如何遍歷單元格,有沒有代表呢?答案是否定的
您必須手動構建indexPaths來從表的單元格。
這是一個簡單的步驟。你的設置是非常正確的。你應該在你的ViewController中有一個tableview。所以,桌面視圖的IBOutlet應該在那裏。我將我的TableView命名爲「myTableView」。而textField的Outlet應該位於TableViewCell中,這也是正確的。最後,Enter按鈕的操作方法應該在視圖控制器中。
確保您正確連接所有插座。
這裏是來樣定製TableViewCell -
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var internalTextField : UITextField!
override func awakeFromNib() {
super.awakeFromNib()
}
}
而現在只是去ViewController.swift-
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var myTableView : UITableView!
var numberOfCells = 2 //you can update it to be any number
override func viewDidLoad() {
super.viewDidLoad()
self.myTableView.dataSource! = self //assign the delegate
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numberOfCells
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as! CustomTableViewCell
return cell;
}
@IBAction func pressedEnter(){
var row = 0
while row < numberOfCells { //iterate through the tableview's cells
let indexPath : NSIndexPath = NSIndexPath(forRow: row, inSection: 0) //Create the indexpath to get the cell
let cell : CustomTableViewCell = self.myTableView.cellForRowAtIndexPath(indexPath) as! CustomTableViewCell
if cell.internalTextField.text!.isEmpty{
print("TextField is Cell \(row) is Empty")
}
else{
print("TextField is Cell \(row) is NOT Empty")
}
row += 1
}
}
}
有這說明了一切評論。希望這可以幫助。
可以這樣做@IBAction func enquireButton(sender:AnyObject)let cell = tableView.dequeueReusableCellWithIdentifier(「Guest Details」)as! GuestDetailsTableViewCell;讓fullName1 = cell.fullNameTextField.text! } –