说明
:在swift实现tableview分组-防微信通讯录的实现的基础上进行。
1. 在员工分组列表页点击某一单元格进入对应的详情页
2. 列表页到对应详情页通用方法
备注
:重点在界面传值,此处传递的是选中的单元格对应的数据对象
(1)StaffManageTableViewController.swift
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if segue.identifier == "showStaffDetail" {
let row = tableView.indexPathForSelectedRow!.row
let destination = segue.destination as! StaffEditViewController
destination.staff = staffs[row]//转场传值
}
}
(2)StaffEditViewController.swift
//
// StaffEditViewController.swift
// JackUChat
//
// Created by 徐云 on 2019/2/19.
// Copyright © 2019 Liy. All rights reserved.
//
import UIKit
class StaffEditViewController: UIViewController {
var staff : Staff!
@IBOutlet weak var inputNameField: UITextField!
@IBOutlet weak var inputPhoneField: UITextField!
@IBOutlet weak var inputPasswordField: UITextField!
@IBOutlet weak var selectPositionBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
showStaffInfo(staff: staff)
}
func showStaffInfo(staff:Staff) {
inputNameField.text = staff.staffName
inputPhoneField.text = staff.staffPhone
inputPasswordField.text = ""
let staffDept = staff.staffDept
selectPositionBtn.setTitle(getPosition(key: staffDept), for: UIControl.State.init(rawValue: 0))
}
func getPosition(key:String) -> String {
var positionDict = ["1":"管理员","2":"班组长","3":"机修","4":"财务","5":"员工"]
guard let positionValue = positionDict[key] else { return "职位未知" }
return positionValue
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
3. 问题描述
列表页到详情页对应的数据对象错位
4. 问题分析
tableview分组列表和一般的tableview列表相比,在获取当前点击的单元格时,不仅要考虑所在的当前行,还要考虑所在的当前块。
5.问题解决
修改StaffManageTableViewController.swift
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if segue.identifier == "showStaffDetail" {
let section = tableView.indexPathForSelectedRow!.section
let sectionTitle = getSectionTitleBySection(section: section)
let staffs:[Staff] = getSectionStaffsBySectionTitle(sectionTitle: sectionTitle)
let row = tableView.indexPathForSelectedRow!.row
print("当前选中第" + section.description + "块,第" + row.description + "行")
let destination = segue.destination as! StaffEditViewController
destination.staff = staffs[row]//转场传值
}
}