美文网首页
通讯录(两个界面)

通讯录(两个界面)

作者: 摇耳朵的小布丁i | 来源:发表于2016-12-02 08:09 被阅读0次

Contact

import UIKitclass Contact: NSObject {    

    var name:String!  

  var gender:String!  

  var phone:String!   

     override init() {      

      } 

   init(dict:Dictionary) {

self.name = dict["name"]

self.gender = dict["gender"]

self.phone = dict["phone"]

}

}

ContactListViewController

import UIKit

class ContactListViewController: UITableViewController {

//定义重用标识

let girlCell = "girlCell"

let boyCell = "boyCell"

//定义字典,作为数据源

var contactSource:[String:[Contact]] = Dictionary()

//存放排好序的key值

var keysArray:[String] = Array()

override func viewDidLoad() {

super.viewDidLoad()

//调用制造数据源方法

self.creatData()

//注册cell

self.tableView.register(GirlStudentCell.self, forCellReuseIdentifier: girlCell)

self.tableView.register(BoyStudentCell.self, forCellReuseIdentifier: boyCell)

self.navigationItem.rightBarButtonItem = self.editButtonItem

}

//创造数据的方法

func creatData(){

let dic =  [

"W":[

["name":"王哲磊","gender":"男","phone":"13525704818"],

["name":"王浩","gender":"男","phone":"13525704817"],

["name":"王晓红","gender":"女","phone":"13525704828"],

["name":"王琳","gender":"女","phone":"13525704898"]

],

"C":[

["name":"陈杨","gender":"女","phone":"13525704898"],

["name":"陈芮","gender":"女","phone":"13525704857"]

],

"B":[

["name":"边文达","gender":"男","phone":"13525724818"],

["name":"宝音","gender":"女","phone":"13525704217"],

["name":"白婧","gender":"女","phone":"13529704828"]

],

"L":[

["name":"李玲","gender":"女","phone":"13725704818"],

["name":"刘二蛋","gender":"男","phone":"13529704817"],

["name":"刘婧","gender":"女","phone":"13525714828"],

["name":"刘福宏","gender":"女","phone":"13525794898"]

]

]

//从dict数据源中把数据读入到contactSource中

for key in dic.keys{

//取出数组对象

let array = dic[key]

//创建数组存储联系人对象

var group:[Contact] = Array()

for dictionary in array!{

//使用字典初始化联系人对象

let aContact = Contact(dict: dictionary)

//将联系人对象添加到数组

group.append(aContact)

}

//使用数据源存储数据

contactSource[key] = group

}

//print(contactSource)

//将字典中的key值排序

let keys = contactSource.keys

//对字典中的key值排序

keysArray = keys.sorted()

// print(keysArray)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

// MARK: - Table view data source

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

//等于键值对的个数

return contactSource.count

}

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

//根据分区下标找到对应的key值

let key = keysArray[section]

//根据key值找到分组

let group = contactSource[key]

//分组中元素的个数就是分区中cell的个数

return (group?.count)!

}

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

let key = keysArray[indexPath.section]

let group = contactSource[key]

//根据cell的下标取出数组对应位置数据模型联系人

let aContact = group?[indexPath.row]

if aContact?.gender == "男"{

let cell = tableView.dequeueReusableCell(withIdentifier: boyCell) as! BoyStudentCell

//调用给cell赋值的方法

cell.setValueViews(aContact: aContact!)

return cell

}else{

let cell = tableView.dequeueReusableCell(withIdentifier: girlCell) as! GirlStudentCell

cell.setValueByContact(contact: aContact!)

return cell

}

}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

return 100

}

//区头

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

return keysArray[section]

}

//索引栏

override func sectionIndexTitles(for tableView: UITableView) -> [String]? {

return keysArray

}

//允许哪些cell可以编辑

// Override to support conditional editing of the table view.

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

return true

}

//设置编辑样式

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {

//第一个分区插入,其他分区的编辑样式都是删除

return indexPath.section == 0 ? .insert : .delete

}

//提交编辑操作

// Override to support editing the table view.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

let key = keysArray[indexPath.section]

var group = contactSource[key]

if editingStyle == .delete {

//删除一个分区

if group?.count == 1{

//先修改数据源

keysArray.remove(at: indexPath.section)

//根据key值删除键值对

contactSource.removeValue(forKey: key)

//再更新UI

let set = NSIndexSet(index: indexPath.section)

tableView.deleteSections(set as IndexSet, with: .left)

}else{

//逐条删除cell

//修改数据源

group?.remove(at: indexPath.row)

//对contactSource从新赋值

contactSource[key] = group

//更新UI

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

}

} else if editingStyle == .insert {

//创建联系人对象

let aContact = Contact()

aContact.name = "宝宝"

aContact.gender = "女"

aContact.phone = "10086"

group?.append(aContact)

//重新赋值

contactSource[key] = group

//刷新UI

tableView.insertRows(at: [indexPath], with: .left)

//重新加载数据

tableView.reloadData()

}

}

//移动哪些cell

// Override to support rearranging the table view.

override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

//修改数据源

let key = keysArray[fromIndexPath.section]

var group = contactSource[key]

//取出原位置的对象

let aContact = group?[fromIndexPath.row]

//删除对象

group?.remove(at: fromIndexPath.row)

//插入到移动之后的位置

group?.insert(aContact!, at: to.row)

//重新赋值

contactSource[key] = group

}

//选择cell时触发的方法

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

let detailVC = DetailViewController()

let key = keysArray[indexPath.section]

let group = contactSource[key]

let aContact = group?[indexPath.row]

detailVC.aContact = aContact

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

}

//移动

// Override to support conditional rearranging of the table view.

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {

// Return false if you do not want the item to be re-orderable.

return true

}

//限制跨区移动的方法

override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {

if sourceIndexPath.section == proposedDestinationIndexPath.section{

return proposedDestinationIndexPath

}else{

return sourceIndexPath

}

}

DetailViewController

import UIKit

class DetailViewController: UIViewController {

var aContact:Contact!

var photoLabel:UIImageView!

var nameLabel:UILabel!

var phoneLabel:UILabel!

override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = #colorLiteral(red: 1, green: 0.8392077942, blue: 0.9732400577, alpha: 1)

self.setupSubviews()

}

func setupSubviews(){

self.setUpData()

if aContact.gender == "男"{

photoLabel.image = #imageLiteral(resourceName: "lr.png")

}

else{

photoLabel.image = #imageLiteral(resourceName: "poto.png")

}

nameLabel.text = aContact.name

nameLabel.text = aContact.phone

}

func setUpData()  {

photoLabel = UIImageView(frame: CGRect(x: 150, y: 100, width: 150, height: 150))

photoLabel.layer.cornerRadius = 75

photoLabel.clipsToBounds = true

self.view.addSubview(photoLabel)

nameLabel = UILabel(frame: CGRect(x: 100, y: 300, width: 250, height: 50))

nameLabel.backgroundColor = UIColor.red

self.view.addSubview(nameLabel)

phoneLabel = UILabel(frame: CGRect(x: 100, y: 380, width: 250, height: 50))

phoneLabel.backgroundColor = UIColor.red

self.view.addSubview(phoneLabel)

}

BoyStudentCell

import UIKit

class BoyStudentCell: UITableViewCell {

var nameLabel:UILabel!

var photoPic:UIImageView!

var saysLabel:UILabel!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

self.setupViews()

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

func setupViews(){

//nameLabel

nameLabel = UILabel(frame: CGRect(x: 10, y: 5, width: kScreenWidth - 120, height: 40))

nameLabel.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)

self.contentView.addSubview(nameLabel)

//photoPic

photoPic = UIImageView(frame: CGRect(x: kScreenWidth - 110, y: 5, width: 90, height: 90))

// photoPic.backgroundColor = #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)

self.contentView.addSubview(photoPic)

//saysLabel

saysLabel = UILabel(frame: CGRect(x: 10, y: 50, width: kScreenWidth - 120, height: 40))

//saysLabel.backgroundColor = #colorLiteral(red: 0.6136813675, green: 0.6465901778, blue: 1, alpha: 1)

self.contentView.addSubview(saysLabel)

}

func setValueViews(aContact:Contact){

self.photoPic.image = UIImage(named:"lr")

self.nameLabel.text = aContact.name

self.saysLabel.text = aContact.phone

}

override func layoutSubviews()

{

super.layoutSubviews()

self.backgroundColor = UIColor.clear;

for view in self.subviews {

self.backgroundView?.frame = CGRect(x:0, y:0, width:(self.backgroundView?.frame.size.width)!,height: (self.backgroundView?.frame.size.height)!);

if NSStringFromClass(view.classForCoder)  == "UITableViewCellDeleteConfirmationView" { // move delete confirmation view

self.bringSubview(toFront: view)

}

}

}

GirlStudentCell

import UIKit

//获取屏幕的宽和高

let kScreenWidth = UIScreen.main.bounds.width

let kScreenheight = UIScreen.main.bounds.height

class GirlStudentCell: UITableViewCell {

//属性定义成私有的

private var photoView:UIImageView!

private var nameLabel:UILabel!

private var saysLabel:UILabel!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

self.setupView()

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

func setupView(){

photoView = UIImageView(frame: CGRect(x: 10, y: 5, width: 90, height: 90))

photoView.layer.cornerRadius = 45

photoView.clipsToBounds = true

// photoView.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)

self.contentView.addSubview(photoView)

nameLabel = UILabel(frame: CGRect(x: 110, y: 5, width: kScreenWidth - 120, height: 40))

nameLabel.backgroundColor = #colorLiteral(red: 1, green: 0.8360928114, blue: 0.8408111242, alpha: 1)

self.contentView.addSubview(nameLabel)

saysLabel = UILabel(frame: CGRect(x: 110, y: 50, width: kScreenWidth - 120, height: 40))

//saysLabel.backgroundColor = #colorLiteral(red: 0.8642925127, green: 0.6110214953, blue: 0.6692668104, alpha: 1)

self.contentView.addSubview(saysLabel)

}

// 封装一个赋值的方法 ---> 接口

func setValueByContact(contact:Contact){

nameLabel.text = contact.name

photoView.image = UIImage(named:"poto")

saysLabel.text = contact.phone

}

//(此方法可不实现)

override func layoutSubviews()

{

super.layoutSubviews()

self.backgroundColor = UIColor.clear;

for view in self.subviews {

self.backgroundView?.frame = CGRect(x:0, y:0, width:(self.backgroundView?.frame.size.width)!,height: (self.backgroundView?.frame.size.height)!);

if NSStringFromClass(view.classForCoder)  == "UITableViewCellDeleteConfirmationView" { // move delete confirmation view

self.bringSubview(toFront: view)

}

}

}

相关文章

  • 通讯录(两个界面)

    Contact import UIKitclass Contact: NSObject { var name...

  • 2019-02-23

    今天突发奇想,把手机A界面APP位置做了调整。 原先,手机两个界面。 主界面是这么几类:通讯录、拨号、短信等手机基...

  • IOS"探望"->通讯录

    在ios中,有两个框架可以访问用户的通讯录 AddressBookUI.framework提供了联系人列表界面, ...

  • 获取通讯录-AddressBook

    获取系统的通讯录,自定义界面

  • 通讯录界面效果

    动态效果 前两天有个朋友问我微信中的通讯录界面怎么实现的,然后写了个Demo给他参考,整理了下,主要难点在于:中文...

  • ★朗姿青春日记每月下单流程

    ①找到通讯录界面,点击公众号。 ② 找到 朗姿青春日记 ③点击3.0...

  • iOS最新获取手机通讯录方式

    一、iOS 9 以前的通讯录框架 AddressBookUI框架:提供了联系人列表界面、联系人详情界面、添加联系人...

  • iOS端获取手机通讯录手机号码

    一、iOS 9 以前的通讯录框架 AddressBookUI框架:提供了联系人列表界面、联系人详情界面、添加联系人...

  • iOS9以后获取手机通讯录

    一、iOS 9 以前的通讯录框架AddressBookUI框架:提供了联系人列表界面、联系人详情界面、添加联系人界...

  • 通讯录知识点

    通讯录知识点 1.演示私人通讯录 学习目的:控制器跳转,控制器之间传值,数据存储 2.项目演示:(4个界面,交给4...

网友评论

      本文标题:通讯录(两个界面)

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