Chr*_*sen 3 xcode uitableview swift swift3
所以我有这个UITableView单元格有4个UITextField,我想在按钮点击时得到它们的值.
此代码不检索任何值.
@IBAction func printBtnAction(_ sender: Any) {
let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell
let alias = cell.aliasTextField.text!
let primaryPhone = cell.primaryPhoneTextField.text!
let secondaryPhone = cell.seondaryPhoneTextField.text!
let email = cell.emailAddressTextField.text!
print("Alias: \(alias), Phone: \(primaryPhone), Phone2: \(secondaryPhone), Email: \(email)")
}
Run Code Online (Sandbox Code Playgroud)
Chr*_*sen 15
我终于通过这个简单的解决方案找到了答案.
@IBAction func saveBtnAction(_ sender: Any) {
let index = IndexPath(row: 0, section: 0)
let cell: AddFriendC1Cell = self.myTableView.cellForRow(at: index) as! AddFriendC1Cell
self.alias = cell.aliasTextField.text!
self.primaryPhone = cell.primaryPhoneTextField.text!
self.secondaryPhone = cell.seondaryPhoneTextField.text!
self.email = cell.emailAddressTextField.text!
print("Alias: \(self.alias), Phone: \(self.primaryPhone), Phone2: \(self.secondaryPhone), Email: \(self.email)")
}
Run Code Online (Sandbox Code Playgroud)
好.按下按钮时,您将使tableViewCell出列.
let cell = self.myTableView.dequeueReusableCell(withIdentifier: "manualAddC1") as! AddFriendC1Cell
Run Code Online (Sandbox Code Playgroud)
我相信您希望获得对现有tableViewCell的引用,如下所示:
if let cell = tableView.cellForRow(at: indexPath) as? AddFriendC1Cell {
// do what you need with cell
}
Run Code Online (Sandbox Code Playgroud)
虽然这可行,但我不建议采用这种方法.如果用户使用像iPhone 4这样的小屏幕手机并且屏幕上看不到某些单元格会怎么样?我认为在那种情况下tableView.cellForRow会返回nil非可见细胞.
我建议textField:shouldChange在每个单元格中使用textView 实现,并使用tableView的viewController委托.当在单元格中更改文本时,委托应将更改传播到viewController,这将在实例变量中保存值.
然后,当您按下按钮时,您只需从实例变量中获取值.
| 归档时间: |
|
| 查看次数: |
17584 次 |
| 最近记录: |