【问题标题】:How to get all UITextField values in UITableView?如何获取 UITableView 中的所有 UITextField 值?
【发布时间】:2019-04-09 13:46:04
【问题描述】:

我正在使用 Core Data。我有一个名为 Users 的核心数据实体。我在 UITableView 中列出 Users。 UITableView 启用了多选。 UITableViewCell 子类包括 username 的 UILable 和 price 的 UITextField。另外,我有一个名为 Expenses 的核心数据实体。

我要做的是:我将为用户输入价格,并将用户信息和价格保存到费用。例如:

  1. 我在 UITableView 中列出了 10 个用户。 (完成

  2. 所有单元格都有用于价格的 UITextField。 (完成

  3. 我在 UITextField 中为我选择的 5 个用户输入价格。 (完成

  4. 从选定的单元格中获取包含 userUID 和价格的信息。 (失败

  5. 使用 doneButtonuserUIDprice 保存到 Expenses

这是我的核心数据实体的样子:

用户:

@NSManaged public var userName: String
@NSManaged public var userUID: String

费用:

@NSManaged public var userUID: String
@NSManaged public var price: Int

class viewController : UIViewController, UITableViewDelegate,UITableViewDataSource {

 @IBOutlet weak var tableView: UITableView!

 var fetchedResultsController : 
    NSFetchedResultsController<Users>!


  override func viewDidLoad() {
        super.viewDidLoad()
        tableView.allowsMultipleSelection = true 
  }


  override func viewWillAppear(_ animated: Bool) {

        fetchUsers() 

  }

  func fetchUsers() {
     ...... // FETCHING USERS FROM COREDATA
     tableView.reloadData()
  }

  @IBAction func doneButton(_ sender: UIBarButtonItem) {
     // ????????????
  }


 func numberOfSections(in tableView: UITableView) -> Int {
        guard let fetchRes  =  fetchedResultsController , let sections 
    = fetchRes.sections else {
            return 0
        }
        return sections.count
    }

 func tableView(_ tableView: UITableView, numberOfRowsInSection 
 section: Int) -> Int {

        guard let fetchRes  =  fetchedResultsController , let 
 sectionInfo = fetchRes.sections?[section] else {
            return 0
        }
        return sectionInfo.numberOfObjects
    }

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
 IndexPath) -> UITableViewCell { 

        let cell = tableView.dequeueReusableCell(withIdentifier: 
          "Cell", for: indexPath) as! CustomCell

        let User = fetchedResultsController.object(at: indexPath)

        cell.lblUserName.text = User.userName

    return cell
 }

}


class CustomCell : UITableViewCell {


    @IBOutlet weak var lblUserName: UILabel!
    @IBOutlet weak var txtPrice: UITextField!


}

我想做的是,从选定单元格的 UITextField 中获取 userUID 和价格。

【问题讨论】:

    标签: swift uitableview core-data


    【解决方案1】:

    问题是你不应该使用 UITableViewCell 作为数据存储,因为如果它们不在屏幕上,TableView 可以从内存中释放它们。

    一个选项在 ViewController 中有数据存储,并将对它的引用设置为单元格。 UITextField 在这个单元格中比设置值直接存储到这个数据中。

    因此,使用完成按钮,您可以从数据存储中读取值。

    【讨论】:

      【解决方案2】:

      从我的观点来看,您需要为单元格数据增加一层类似的东西

      class CellViewModel {
          let user: User
          var price: Int/Double/String
      }
      

      获取用户后,您将创建 CellViewModel 列表并保留它。并且您的 CustomCell 可以使用它(设置名称等)。

      单元格应为UITextFieldDelefate,并将price 更改为CellViewModel

      完成所有更改后,您会从indexPathsForSelectedRows 中获取选定单元格的列表。现在您有了所选单元格的索引和CellViewModel 的列表。现在你遍历它们并创建你的Expenses

      检查列表中的mapfilter 方法,以使您的解决方案更具可读性。

      祝你好运

      【讨论】:

      • 感谢您的回答。我使用协议解决了 textFieldDidEndEditing 的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多