[英]Check textField in each row of tableView
這是這種情況:我有一個包含3行的tableView。 在此tableView中,有一個帶有textField的tableVIewCell原型。 此textField用於tableView的每一行(不同)。 我的問題是:如何檢查每個textField是否為空? 我猜是一個If語句,但我不明白如何分別檢查所有textField。 有人可以幫助我嗎?
我的tableView的示例: http : //imagizer.imageshack.us/a/img540/2130/UsUNO3.png
過去,我不得不做這件事,這是我使用的方法:
for index in 0...(cellCount - 1) {
var indexpath = NSIndexPath(forRow: index, inSection: 0)
if let cellAtIndex = tableView.cellForRowAtIndexPath(indexpath) as? ContestantsTableViewCell {
var newContestantString = cellAtIndex.contestantNameField.text
if !newContestantString.isEmpty {
contestants.append(newContestantString)
}
}
}
我只是使用cellForRowAtIndexPath
在索引路徑中獲取單元格,並通過將其強制轉換為自定義類型從單元格中獲取屬性。
如果您知道始終只顯示3個單元格,則可以保留對每個單元格和每個單元格textField的引用,並在需要時檢查其長度。
您可以使用indexpath.row(Tableview)在每一行中檢查文本字段
if(indexpath.row == 0){
if(cell.textfield.text == "")
{
//1st textfiled empty
}
}
else if(indexpath.row == 1){
if(cell.textfield.text == "")
{
//2nd textfiled empty
}
}
else if(indexpath.row == 2){
if(cell.textfield.text == "")
{
//3rd textfiled empty
}
}
好吧,你可以創建功能類似below.You可以使用該功能為任意數量的電池不使用if else
條件。 您也可以將此功能用作textField驗證。
func getCellsDataOn() -> Bool {
for section in 0 ..< self.tableView.numberOfSections {
for row in 0 ..< self.tableView.numberOfRows(inSection: section) {
let indexPath = NSIndexPath(row: row, section: section)
let cell = self.tableView.cellForRow(at: indexPath as IndexPath) as! YourCell
if (cell.txtField!.text! != "") {
print("Not Empty")
//Handle your function
}else {
print("Empty")
return false
}
}
}
return true
}
希望對您有所幫助謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.