美文网首页IOS
swift实现tableview分组-列表页到详情页错位

swift实现tableview分组-列表页到详情页错位

作者: 瑟闻风倾 | 来源:发表于2019-02-19 11:23 被阅读0次

说明:在swift实现tableview分组-防微信通讯录的实现的基础上进行。

1. 在员工分组列表页点击某一单元格进入对应的详情页

员工分组列表页.png
详情编辑页.png

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]//转场传值
        }
    }

相关文章

网友评论

    本文标题:swift实现tableview分组-列表页到详情页错位

    本文链接:https://www.haomeiwen.com/subject/rejxyqtx.html