将tableviewcell中的数据显示到Pop-up UIView中
Display data from tableviewcell into Pop-up UIView
我有一个 tableView 显示用户已经创建的多个任务。当用户单击任务(tableView 单元格)时,我想显示一个弹出窗口,显示有关用户任务的更多信息。我已经创建了弹出视图,它显示正常,但数据(类别、日期、小时)没有显示在弹出窗口中。当用户单击该行时,我不知道如何访问我的数据并将其放入弹出视图中。到目前为止,这是我尝试过的:
MyTasksCollectionCell
enum DisplayedTasks {
case current
case past
}
class MyTasksCollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
var displayedTasks = DisplayedTasks.current
var tasks = [Add]()
var pastTasks = [Add]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! MyTasksTableCell
cell.accessoryType = .disclosureIndicator
let task = {() -> Add in
switch (displayedTask) {
case .current:
// First segment tapped
return self.tasks[indexPath.row]
case past:
// Second segment tapped
return self.pastTasks[indexPath.row]
}
}()
cell.categoryLabel.text =
"\(task.category)"
cell.dateLabel.text =
"\(task.date)"
cell.hourLabel.text =
"\(task.hour)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let task = {() -> Add in
switch (displayedTask) {
case .current:
// First segment tapped
return self.tasks[indexPath.row]
case past:
// Second segment tapped
return self.pastTasks[indexPath.row]
}
}()
let selectedCell = tableView.cellForRow(at: indexPath) as? MyTasksDetailView//Warning message here !!! "Cast from 'UITableViewCell?' to unrelated type 'MyTasksDetailView' always fails"
selectedCell?.category.text = "\(task.category)"
selectedCell?.hour.text = "\(task.hour)"
selectedCell?.date.text = "\(task.date)"
print (selectedCell?.category.text)
let popupView = MyTasksDetailView()
UIApplication.shared.keyWindow?.addSubview(popupView)
}
extension UIApplication {
var keyWindow: UIWindow? {
// Get connected scenes
return UIApplication.shared.connectedScenes
// Keep only active scenes, onscreen and visible to the user
.filter { [=10=].activationState == .foregroundActive }
// Keep only the first `UIWindowScene`
.first(where: { [=10=] is UIWindowScene })
// Get its associated windows
.flatMap({ [=10=] as? UIWindowScene })?.windows
// Finally, keep only the key window
.first(where: \.isKeyWindow)
}
}
MyTasksDetailView
class MyTasksDetailView: UIView {
var setCategory: String? {
didSet {
categoryLabel.text = setCategory ?? ""
}
}
var setDate: String? {
didSet {
dateLabel.text = setDate ?? ""
}
}
var setHour: String? {
didSet {
hourLabel.text = setHour ?? ""
}
}
let categoryLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 28, weight: .bold)
// label.text = "category"
label.textAlignment = .center
label.textColor = .label
return label
}()
let dateLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
// label.text = "date"
label.textAlignment = .center
label.numberOfLines = 3
label.textColor = .label
return label
}()
let hourLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
// label.text = "hour"
label.textAlignment = .center
label.numberOfLines = 3
label.textColor = .label
return label
}()
let container: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.clipsToBounds = true
v.backgroundColor = .white
v.layer.cornerRadius = 24
v.backgroundColor =
// 1
UIColor { traitCollection in
// 2
switch traitCollection.userInterfaceStyle {
case .dark:
// 3
v.layer.borderColor = UIColor.label.cgColor
return UIColor.systemBackground
default:
// 4
v.layer.borderColor = UIColor.black.cgColor
return UIColor.systemBackground
}
}
return v
}()
lazy var stack: UIStackView = {
let stack = UIStackView(arrangedSubviews: [categoryLabel, dateLabel, hourLabel])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
return stack
}()
@objc func animateOut() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.container.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
self.alpha = 0
}) { (complete) in
if complete {
self.removeFromSuperview()
}
}
}
@objc func animateIn() {
self.container.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.container.transform = .identity
self.alpha = 1
})
}
override init(frame: CGRect) {
super.init(frame: frame)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(animateOut)))
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = bounds
blurEffectView.alpha = 1
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
self.frame = UIScreen.main.bounds
self.addSubview(container)
container.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
container.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
container.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.7).isActive = true
container.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.45).isActive = true
container.addSubview(stack)
stack.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
stack.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
stack.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
stack.heightAnchor.constraint(equalTo: container.heightAnchor, multiplier: 0.5).isActive = true
animateIn()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
添加(数据结构)
struct Add {
static var details: Add = Add()
var category: String = ""
var date: String = ""
var hour: String = ""
var id: String?
func getDict() -> [String: Any] {
let dict = ["category": self.category,
"date": self.date,
"hour": self.hour,
] as [String : Any]
return dict
}
}
我看不到你在 MyTasksDetailView 中设置所需数据的位置。
将 属性“任务”添加到 MyTasksDetailView(无法找到您的任务实例的类型,因此在我的示例中我使用了任务)
internal var task: Task? {
didSet {
if let task = task {
setCategory = task.category
setDate = task.date
setHour = task.hour
}
}
}
并在初始化后设置
let popupView = MyTasksDetailView()
popupView.task = task
UIApplication.shared.keyWindow?.addSubview(popupView)
我有一个 tableView 显示用户已经创建的多个任务。当用户单击任务(tableView 单元格)时,我想显示一个弹出窗口,显示有关用户任务的更多信息。我已经创建了弹出视图,它显示正常,但数据(类别、日期、小时)没有显示在弹出窗口中。当用户单击该行时,我不知道如何访问我的数据并将其放入弹出视图中。到目前为止,这是我尝试过的:
MyTasksCollectionCell
enum DisplayedTasks {
case current
case past
}
class MyTasksCollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
var displayedTasks = DisplayedTasks.current
var tasks = [Add]()
var pastTasks = [Add]()
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! MyTasksTableCell
cell.accessoryType = .disclosureIndicator
let task = {() -> Add in
switch (displayedTask) {
case .current:
// First segment tapped
return self.tasks[indexPath.row]
case past:
// Second segment tapped
return self.pastTasks[indexPath.row]
}
}()
cell.categoryLabel.text =
"\(task.category)"
cell.dateLabel.text =
"\(task.date)"
cell.hourLabel.text =
"\(task.hour)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let task = {() -> Add in
switch (displayedTask) {
case .current:
// First segment tapped
return self.tasks[indexPath.row]
case past:
// Second segment tapped
return self.pastTasks[indexPath.row]
}
}()
let selectedCell = tableView.cellForRow(at: indexPath) as? MyTasksDetailView//Warning message here !!! "Cast from 'UITableViewCell?' to unrelated type 'MyTasksDetailView' always fails"
selectedCell?.category.text = "\(task.category)"
selectedCell?.hour.text = "\(task.hour)"
selectedCell?.date.text = "\(task.date)"
print (selectedCell?.category.text)
let popupView = MyTasksDetailView()
UIApplication.shared.keyWindow?.addSubview(popupView)
}
extension UIApplication {
var keyWindow: UIWindow? {
// Get connected scenes
return UIApplication.shared.connectedScenes
// Keep only active scenes, onscreen and visible to the user
.filter { [=10=].activationState == .foregroundActive }
// Keep only the first `UIWindowScene`
.first(where: { [=10=] is UIWindowScene })
// Get its associated windows
.flatMap({ [=10=] as? UIWindowScene })?.windows
// Finally, keep only the key window
.first(where: \.isKeyWindow)
}
}
MyTasksDetailView
class MyTasksDetailView: UIView {
var setCategory: String? {
didSet {
categoryLabel.text = setCategory ?? ""
}
}
var setDate: String? {
didSet {
dateLabel.text = setDate ?? ""
}
}
var setHour: String? {
didSet {
hourLabel.text = setHour ?? ""
}
}
let categoryLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 28, weight: .bold)
// label.text = "category"
label.textAlignment = .center
label.textColor = .label
return label
}()
let dateLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
// label.text = "date"
label.textAlignment = .center
label.numberOfLines = 3
label.textColor = .label
return label
}()
let hourLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
// label.text = "hour"
label.textAlignment = .center
label.numberOfLines = 3
label.textColor = .label
return label
}()
let container: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.clipsToBounds = true
v.backgroundColor = .white
v.layer.cornerRadius = 24
v.backgroundColor =
// 1
UIColor { traitCollection in
// 2
switch traitCollection.userInterfaceStyle {
case .dark:
// 3
v.layer.borderColor = UIColor.label.cgColor
return UIColor.systemBackground
default:
// 4
v.layer.borderColor = UIColor.black.cgColor
return UIColor.systemBackground
}
}
return v
}()
lazy var stack: UIStackView = {
let stack = UIStackView(arrangedSubviews: [categoryLabel, dateLabel, hourLabel])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
return stack
}()
@objc func animateOut() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.container.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
self.alpha = 0
}) { (complete) in
if complete {
self.removeFromSuperview()
}
}
}
@objc func animateIn() {
self.container.transform = CGAffineTransform(translationX: 0, y: -self.frame.height)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseIn, animations: {
self.container.transform = .identity
self.alpha = 1
})
}
override init(frame: CGRect) {
super.init(frame: frame)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(animateOut)))
let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = bounds
blurEffectView.alpha = 1
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
self.frame = UIScreen.main.bounds
self.addSubview(container)
container.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
container.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
container.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.7).isActive = true
container.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.45).isActive = true
container.addSubview(stack)
stack.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
stack.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
stack.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
stack.heightAnchor.constraint(equalTo: container.heightAnchor, multiplier: 0.5).isActive = true
animateIn()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
添加(数据结构)
struct Add {
static var details: Add = Add()
var category: String = ""
var date: String = ""
var hour: String = ""
var id: String?
func getDict() -> [String: Any] {
let dict = ["category": self.category,
"date": self.date,
"hour": self.hour,
] as [String : Any]
return dict
}
}
我看不到你在 MyTasksDetailView 中设置所需数据的位置。
将 属性“任务”添加到 MyTasksDetailView(无法找到您的任务实例的类型,因此在我的示例中我使用了任务)
internal var task: Task? {
didSet {
if let task = task {
setCategory = task.category
setDate = task.date
setHour = task.hour
}
}
}
并在初始化后设置
let popupView = MyTasksDetailView()
popupView.task = task
UIApplication.shared.keyWindow?.addSubview(popupView)