【澳门葡京备用网址】代码创造非凡分界面包车型客车九宫图,swift轻巧代码记录

在上①篇只是简短达成了九宫图效果,本章供给形成APP分界面玖宫图效果

在上一篇只是轻易完毕了9宫图效果,本章须要产生应用软件分界面玖宫图效果

发现会OC的
看swift依旧对比轻巧的,只是不要分号很不习惯,仅此记录下学习时候写的简便代码

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”]

}

}

 

 

创建按键

let button = UIButton(type: .custom)

button.frame = CGRect(x: 100, y: 100, width: 100, height: 100)

view.addSubview(button)

button.backgroundColor = UIColor.blue

button .addTarget(self, action: #selector(click), for:
.touchUpInside)

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中

【澳门葡京备用网址】代码创造非凡分界面包车型客车九宫图,swift轻巧代码记录。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

}

}

 override func viewDidLoad() {

 override func viewDidLoad() {

落到实处Button点击方法

func click() -> () {

}

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)

}

        super.viewDidLoad()

        super.viewDidLoad()

创建imageView

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width:
view.frame.size.width, height: view.frame.size.height))

imageView.backgroundColor = UIColor.red

imageView.image = UIImage(named: “timg.jpeg”)

view .addSubview(imageView)

创建tableView

let tab = UITableView(frame:view.frame, style: .plain)

tab.delegate = self

tab.dataSource = self

view.addSubview(tab)

tab.register(UITableViewCell.self, forCellReuseIdentifier: “cell”)

//MARK: delegate

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

return 10

}

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

let cell = tableView .dequeueReusableCell(withIdentifier: “cell”, for:
indexPath)

cell.textLabel?.text = “\(indexPath.row)”

cell.selectionStyle = .none

cell.contentView.backgroundColor = UIColor.brown

return cell

}

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

return 44

}

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

print(“\(indexPath.row)”)

}

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)

}

}

}

        createnine()

        createnine()

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)

}

}

}

    }

    }

 

 

//九宫图算法

//9宫图算法

    func createnine(){

    func createnine(){

//高

//高

        let kAppViewH:CGFloat=80

        let kAppViewH:CGFloat=80

//宽        

//宽        

let kAppViewW:CGFloat=80

let kAppViewW:CGFloat=80

//行个数

//行个数

        let kColCount:Int=3

        let kColCount:Int=3

//间隔 

//间隔 

        let kStart:Int=20

        let kStart:Int=20

         

         

        let
MarginX:CGFloat=(self.view.frame.size.width-CGFloat(kColCount)*kAppViewW)/CGFloat(kColCount+1)

        let
MarginX:CGFloat=(self.view.frame.size.width-CGFloat(kColCount)*kAppViewW)/CGFloat(kColCount+1)

        let MarginY:CGFloat=10

        let MarginY:CGFloat=10

        

        

        for i in 0 ..< 12 {

        for i in 0 ..< 12 {

            let row:Int=i/kColCount;

            let row:Int=i/kColCount;

            let col:Int=i%kColCount;

            let col:Int=i%kColCount;

            let colF:CGFloat=CGFloat(col);

            let colF:CGFloat=CGFloat(col);

            

            

            let x=MarginX+CGFloat(col)*(kAppViewW+MarginX);

            let x=MarginX+CGFloat(col)*(kAppViewW+MarginX);

            let
y=CGFloat(kStart)+MarginY+CGFloat(row)*(kAppViewH+MarginY);

            let
y=CGFloat(kStart)+MarginY+CGFloat(row)*(kAppViewH+MarginY);

            

            

            var imageView=UIImageView(image: UIImage(named: “”))

            var imageView=UIImageView(image: UIImage(named: “”))

            imageView.frame=CGRect(x: 0, y: 0, width: 60, height: 60)

            imageView.frame=CGRect(x: 0, y: 0, width: 60, height: 60)

            //增加Lable

            //增加Lable

            var  nameLabel=UILabel(frame: CGRect(x:0, y: 60, width:
Int(kAppViewW), height: 20))

            var  nameLabel=UILabel(frame: CGRect(x:0, y: 60, width:
Int(kAppViewW), height: 20))

澳门葡京备用网址 ,            nameLabel.text=”功能”

            nameLabel.text=”功能”

            nameLabel.textAlignment=NSTextAlignment.center

            nameLabel.textAlignment=NSTextAlignment.center

            nameLabel.backgroundColor=UIColor.blue

            nameLabel.backgroundColor=UIColor.blue

            

            

            //var btnBuuton=UIButton.button

            //var btnBuuton=UIButton.button

//增加按键 

//扩大按键 

            let btnButton=UIButton(frame: CGRect(x: 0, y: 20, width: 60,
height: 60))

            let btnButton=UIButton(frame: CGRect(x: 0, y: 20, width: 60,
height: 60))

            btnButton.tag=i

            btnButton.tag=i

            btnButton.setTitle(“按钮”, for: UIControlState.normal)

            btnButton.setTitle(“按钮”, for: UIControlState.normal)

            btnButton.addTarget(self, action:
#selector(btnClick(sender:)), for:

            btnButton.addTarget(self, action:
#selector(btnClick(sender:)), for:

                .touchUpInside)

                .touchUpInside)

            let view=UIView(frame: CGRect(x: x, y: y, width: kAppViewW,
height: kAppViewH))

            let view=UIView(frame: CGRect(x: x, y: y, width: kAppViewW,
height: kAppViewH))

            view.backgroundColor=UIColor.red

            view.backgroundColor=UIColor.red

            view.addSubview(imageView)

            view.addSubview(imageView)

            view.addSubview(nameLabel)

            view.addSubview(nameLabel)

            view.addSubview(btnButton)

            view.addSubview(btnButton)

//参与视图

//加入视图

            self.view.addSubview(view)

            self.view.addSubview(view)

        }  

        }  

    }

    }

 

 

 

 

功效如下 

效益如下 

澳门葡京备用网址 1

澳门葡京备用网址 2

 

 

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*
*
Website