swift-联系人显示列表

Swift实现联系人显示列表
本文介绍了如何使用Swift编程语言创建一个联系人显示列表。内容包括相关视图的实现细节和可能遇到的问题。

view我就不写了有空复制,反正这玩意没法套用

//

//  ContactPersonViewController.swift

//  yadaService

//

//  Created by 小海马 on 2018/5/15.

//  Copyright © 2018年 . All rights reserved.

//



import UIKit

import SnapKit



import Alamofire

import SwiftyJSON



class ContactPersonViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    

    //刷新

    var refreshControl = UIRefreshControl()

    

    lazy var tableView:UITableView!=UITableView()

    var contactList : [Contact] = []



    //传值用参数

    var lmidSegue : Int64 = 0

    var nameSegue : String?

    var sexSegue : String?

    var identityCardSegue : String?

    var postSegue : String?

    var telphoneSegue : String?

    var phoneSegue : String?

    var emailSegue : String?

    

    override func loadView() {

        super.loadView()

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "新增", style: .plain, target: self, action: #selector(addBtn))

        

        let urlManager = Urls()

        let customer=UserDefaultsTools.sharedUserDefaults.getCustomer()

        let params : Parameters = ["ctId" : customer.data?.id as Any]

        ContactViewModel.sharedContact.requestContactesInfo(url: urlManager.getContactInfo!, method: .get, parameters: params, finished: {status,contactList in

            self.contactList = contactList

            

            self.tableView?.reloadData()

        })

        

    }

    

    //左滑按钮

    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

        //实现一个收藏按钮

        let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (_, _, completion) in completion(true)

            //数据删除

            let deleteID = self.contactList[indexPath.row].lmid

            let deleteName = self.contactList[indexPath.row].name

            print("删除了id为\(deleteID),地名为\(deleteName!)的数据")

            

            let urlManager = Urls()

            let params : Parameters = ["lmid" : self.contactList[indexPath.row].lmid]

            ContactViewModel.sharedContact.deleteContactInfo(url: urlManager.deleteContactById!, method: .post, parameters: params, finished: {status,contactList in

            })

            

            //删除数组所在行

            self.contactList.remove(at: indexPath.row)

            //界面更新,删除单元格所在行,删除之后是从下往上推的,所以用top

            tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)

        }

        deleteAction.backgroundColor = UIColor(displayP3Red: 225/255, green: 72/255, blue: 72/255, alpha: 1)

        

        //告诉系统一共几个按钮

        let config = UISwipeActionsConfiguration(actions: [deleteAction])

        return config

    }

    

    

    

    

    override func viewDidLoad() {

        super.viewDidLoad()

        

        

        

        //修改标题颜色

        self.navigationController?.navigationBar.titleTextAttributes = [

            NSAttributedStringKey.foregroundColor : UIColor.white

        ]

        

        self.title = "联系人信息"

        

        //按钮颜色

        self.navigationController?.navigationBar.tintColor = UIColor.white

        //滑动时隐藏导航条

        navigationController?.hidesBarsOnSwipe = true

        

        

        

        //设置UITableView的位置

        let rect = self.view.frame

        tableView = UITableView(frame: rect)

        self.tableView.backgroundColor = UIColor(displayP3Red: 54/255, green: 45/255, blue: 66/255, alpha: 1)

        //去掉分割线

        self.tableView!.separatorStyle = .none

        //设置行高

        tableView!.estimatedRowHeight = 240

        tableView.rowHeight = 240

        //设置数据源

        self.tableView!.dataSource = self

        //设置代理

        self.tableView!.delegate = self

        

        self.view.addSubview(self.tableView!)

        

        //注册UITableView,cellID为重复使用cell的Identifier

        self.tableView!.register(ContactPersonCell.self, forCellReuseIdentifier: "tableCell")

        

        // 设置刷新控件

        refreshControl = UIRefreshControl()

        tableView?.addSubview(refreshControl)

        

        refreshControl.addTarget(self, action: #selector(loadData), for: .valueChanged)

    }

    

    //刷新方法

    @objc private func loadData() {

        // 模拟数据延时

        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 1) {

            

            //----------------开始加载数据------------------

            let urlManager = Urls()

            let customer=UserDefaultsTools.sharedUserDefaults.getCustomer()

            let params : Parameters = ["ctId" : customer.data?.id as Any]

            ContactViewModel.sharedContact.requestContactesInfo(url: urlManager.getContactInfo!, method: .get, parameters: params, finished: {status,contactList in

                self.contactList=contactList

                self.tableView?.reloadData()

            })

            

            //----------------结束加载数据------------------

            self.refreshControl.endRefreshing()

        }

    }

    

    //设置cell的数量

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return contactList.count

    }

    

    //设置section的数量,我们这只有一个分区

    func numberOfSections(in tableView: UITableView) -> Int {

        return 1

    }

    

    //设置tableview的cell

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        

        //创建一个重用单元格

        let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! ContactPersonCell

        

        let ctct = contactList[indexPath.row]

        cell.contactPersonName.text = ctct.name

        cell.jobTitleLabel.text = ctct.post

        cell.phoneLabel.text = ctct.telphone

        cell.belongsLabel.text = ctct.ctaddrId

        cell.mobliePhoneLabel.text = ctct.phone

        cell.emailLabel.text = ctct.email

        

        

        

        cell.backgroundColor = UIColor(displayP3Red: 54/255, green: 45/255, blue: 66/255, alpha: 1)

        cell.textLabel?.textColor = UIColor.white

        

        return cell

    }

    

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        tableView.deselectRow(at: indexPath, animated: true)//点击完成,取消高亮

        print("点击的人员id为\(contactList[indexPath.row].lmid)")

        

        // 创建alert

        let alertController = UIAlertController(title: "要修改\((contactList[indexPath.row].name)!)的信息吗", message: nil, preferredStyle:.alert)

        let cancelAction = UIAlertAction(title: "取消", style: .default) { (UIAlertAction) in

        }

        let okAction = UIAlertAction(title: "好的", style: .default) { (UIAlertAction) in

            

            self.lmidSegue = self.contactList[indexPath.row].lmid

            self.nameSegue = self.contactList[indexPath.row].name

            self.sexSegue = self.contactList[indexPath.row].sex

            self.identityCardSegue = self.contactList[indexPath.row].identityCard

            self.postSegue = self.contactList[indexPath.row].post

            self.telphoneSegue = self.contactList[indexPath.row].telphone

            self.phoneSegue = self.contactList[indexPath.row].phone

            self.emailSegue = self.contactList[indexPath.row].email

            //传值

            let appDele = UIApplication.shared.delegate as! AppDelegate

            appDele.ctctLmid = self.contactList[indexPath.row].lmid

            appDele.ctctName = self.contactList[indexPath.row].name

            appDele.ctctSex = self.contactList[indexPath.row].sex

            appDele.ctctIdentityCard = self.contactList[indexPath.row].identityCard

            appDele.ctctPost = self.contactList[indexPath.row].post

            appDele.ctctTelphone = self.contactList[indexPath.row].telphone

            appDele.ctctPhone = self.contactList[indexPath.row].phone

            appDele.ctctEmail = self.contactList[indexPath.row].email

            self.dismiss(animated: true, completion: nil)

            

            //转场

            self.hidesBottomBarWhenPushed = true

            let cpcvc = ContactPersonChangeViewController()

            self.navigationController?.pushViewController(cpcvc, animated: true)

            self.hidesBottomBarWhenPushed = true

        }

        // 添加

        alertController.addAction(cancelAction)

        alertController.addAction(okAction)

        // 弹出

        self.present(alertController, animated: true, completion: nil)

        

    }

    

    override func viewDidAppear(_ animated: Bool) {

        

    }

    

    @objc func addBtn(_ button:UIButton) {

        

        print("点击新增地址")

        //转场

        self.hidesBottomBarWhenPushed = true

        let cpevc = ContactPersonEditViewController()

        self.navigationController?.pushViewController(cpevc, animated: true)

        self.hidesBottomBarWhenPushed = true

    }

    

    

    @objc func saveAndContinue(){

        // 创建

        let alertController = UIAlertController(title: "提交完成", message: nil, preferredStyle:.alert)

        let okAction = UIAlertAction(title: "好的", style: .default) { (UIAlertAction) in

            self.navigationController?.popViewController(animated: true)

            print("提交完成")

        }

        

        // 添加

        alertController.addAction(okAction)

        

        // 弹出

        self.present(alertController, animated: true, completion: nil)

    }

    

    

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值