【问题标题】:How to pass multiple data in single tableview如何在单个tableview中传递多个数据
【发布时间】:2020-02-21 10:08:36
【问题描述】:

您好,我有两个不同的数组数据需要传递给视图控制器,我的 ViewControllers 设计是相同的,但唯一的区别是数据。我怎样才能做到这一点?这是我的代码

var attendance: [GAttendance]!
var subjectAttendances: [GAttendances]!

// In my A controller
let detailAbsenceVC                 = DetailAbsenceVC()
detailAbsenceVC.attendance      = attendances
self.present(detailAbsenceVC, animated: true)

// In my B controller
let detailVC                    = DetailAbsenceVC()
detailVC.subjectAttendances = subjectAttendances
self.present(detailVC, animated: true, completion: nil)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return attendance.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: GStudentAbsenceCell.cellID, for: indexPath) as! GStudentAbsenceCell
    let attendanceItem = attendance[indexPath.row]
    cell.configureCell(attendance: attendanceItem)
    return cell
}

【问题讨论】:

  • 所以tableView 位于DetailAbsenceVC 中,您希望在此处显示任一来自A/B 的attendancesubjectAttendances 数据控制器?
  • 是的@AndreasOetjen

标签: ios arrays swift uitableview


【解决方案1】:

如果你不区分你来自A还是B,你只需要一个数组来存储DetailAbsenceVC中的数据,我们称之为detailData

class DetailAbsenceVC : UIViewController {
    var detailData = [GAttendance]()

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return detailData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: GStudentAbsenceCell.cellID, for: indexPath) as! GStudentAbsenceCell
        let attendanceItem = detailData[indexPath.row]
        cell.configureCell(attendance: attendanceItem)
        return cell
    }
}

然后,在 A/B 控制器中,只需设置 detailData:

// In my A controller
let detailAbsenceVC         = DetailAbsenceVC()
detailAbsenceVC.detailData  = attendances
self.present(detailAbsenceVC, animated: true)

// In my B controller
let detailVC        = DetailAbsenceVC()
detailVC.detailData = subjectAttendances
self.present(detailVC, animated: true, completion: nil)

【讨论】:

  • 嘿,谢谢@AndreasOetjen,它完美地工作,我在想复杂的事情,但解决方案很简单。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-14
  • 1970-01-01
  • 2013-07-28
  • 2021-08-30
  • 2013-10-12
  • 1970-01-01
相关资源
最近更新 更多