【问题标题】:How to add multiple items to a UITableViewCell?如何将多个项目添加到 UITableViewCell?
【发布时间】:2020-06-23 19:48:07
【问题描述】:

谁能帮我让email 字段显示在tableView 单元格中? amount 字段正在显示,但我无法让 email 与它一起显示。

struct Posts {
    var amount:String
    var email:String
}

class PaymentRequestVC: UIViewController, UITableViewDelegate {
    
    let tableView = UITableView()
   
    var posts = [Posts]()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        self.tableView.dataSource = self
        self.tableView.delegate = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell2")
        tableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 900)
        setupViews()
        setupTableView()
        loadPosts()
        self.tableView.reloadData()
        
    
    }

    func loadPosts() {
        let dbUsers = Firestore.firestore().collection("Payouts")
        dbUsers.addSnapshotListener { (querySnapshot, error) in
            if let error = error {
                print("\(error.localizedDescription)")
            } else {
                for document in (querySnapshot?.documents)! {
                    if let Amount = document.data()["amount"] as? String {
                        let Email = document.data()["email"] as? String
                        print(Amount)
                        var post = Posts(amount: "", email: "")
                        post.amount = Amount
                        post.email = Email ?? ""

                        self.posts.append(post)
                    }
                }
                
                    self.tableView.reloadData()
                
                print(self.posts)
            }
        }
      }
    
    private func setupViews() {
    let stackView: UIStackView = {
        let sv = UIStackView()
        sv.translatesAutoresizingMaskIntoConstraints = false
        sv.spacing = 28
        sv.axis = .vertical
        sv.distribution = .fill
        sv.alignment = .fill
        return sv
        }()
        view.addSubview(stackView)
        view.addSubview(tableView)

    }
    
    func setupTableView() {
           
           NSLayoutConstraint.activate([
                  tableView.topAnchor.constraint(equalTo: self.view.topAnchor),
                  tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
                  tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
                  tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor)
              ])
           }
}

extension PaymentRequestVC: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return posts.count
}

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row < posts.count {
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
                let post = posts[indexPath.row]
                cell.textLabel?.text = post.amount
                cell.textLabel?.textColor = UIColor.black
                return cell
            } else {
                let cell2 = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath)
                let post = posts[indexPath.row]
                cell2.detailTextLabel?.text = post.email
                cell2.detailTextLabel?.textColor = UIColor.black
                return cell2
            }
 }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
}

【问题讨论】:

    标签: swift firebase uitableview firebase-realtime-database tableview


    【解决方案1】:

    斯威夫特 5

    您需要使用 UITableViewCell.CellStyle.subtitle 初始化单元格,以便 detailTextLabel 属性起作用。

    在您的扩展程序中,更改 cell init:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if indexPath.row < posts.count {
                let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
                let post = posts[indexPath.row]
                cell.textLabel?.text = post.amount
                cell.textLabel?.textColor = UIColor.black
                cell.detailTextLabel?.text = post.email
                cell.detailTextLabel?.textColor = UIColor.black
                return cell
            } else {
                let cell2 = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell2")
                let post = posts[indexPath.row]
                cell2.detailTextLabel?.text = post.email
                cell2.detailTextLabel?.textColor = UIColor.black
                return cell2
            }
    

    请注意,如果您希望 amountmail 同时显示,使用 else 将仅显示金额或电子邮件, 就像在下面的代码中

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if indexPath.row < posts.count {
                let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
                let post = posts[indexPath.row]
                cell.textLabel?.text = post.amount
                cell.textLabel?.textColor = UIColor.black
                return cell
            } else {
                let cell2 = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell2")
                let post = posts[indexPath.row]
                cell2.detailTextLabel?.text = post.email
                cell2.detailTextLabel?.textColor = UIColor.black
                return cell2
            }
    

    所以更好的方法是同时返回,else什么都不显示

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if indexPath.row < posts.count {
                let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")
                let post = posts[indexPath.row]
                cell.textLabel?.text = post.amount
                cell.textLabel?.textColor = UIColor.black
                cell.detailTextLabel?.text = post.email
                cell.detailTextLabel?.textColor = UIColor.black
                return cell
            } else {
                let cell2 = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell2")
                //Empty cell returned if the condition above is not fulfilled
                return cell2
            }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我认为您在 cellForRowAt 中的逻辑不正确。

      您有一个帖子数组。你想在同一个单元格中显示金额和电子邮件吗?一个标签是标题,另一个是详细文本。

      两个标签都在同一个 UITableViewCell 中,所以每个帖子只需要设置一次。

      此外,如果 indexPath.row 大于帖子计数,则 posts[indexPath.row] 实际上会使您的代码崩溃。所以 else 语句实际上应该只记录一个错误或什么都不做。

      试试这样的:

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          if indexPath.row < posts.count {
                  let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
                  let post = posts[indexPath.row]
                  cell.textLabel?.text = post.amount
                  cell.textLabel?.textColor = UIColor.black
                  cell.textLabel?.detailTextLabel?.text = post.email
                  cell.textLabel?.detailTextLabel?.textColor = UIColor.black
                  return cell
          } else {
                  // No cells
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2022-11-10
        • 1970-01-01
        • 2020-10-05
        • 2022-11-12
        • 1970-01-01
        • 2019-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多