2016年12月11日 星期日 晚
这周总结的较少,时间也比较紧,希望大家见谅,但总结的都是比较重要的部分,希望有助于大家的学习和复习,下周一定会给大家一个更详细的总结,么么哒😘
day15
微信界面
- 新建一个JNTViewController.swift继承自UITabBarController,在其中进行控制器的创建:
override func viewDidLoad() {
super.viewDidLoad()
let vc = ViewController() //创建控制器
self.addChildVC(vc: vc, title: "消息", image: UIImage(named: "tabbar_mainframe")!, selectImage: UIImage(named: "tabbar_mainframeHL")!.withRenderingMode(.alwaysOriginal))
let contact = ContactViewController()
self.addChildVC(vc: contact, title: "联系人", image: UIImage(named: "tabbar_contacts")!, selectImage: UIImage(named: "tabbar_contactsHL")!.withRenderingMode(.alwaysOriginal))
let discover = DiscoverViewController()
self.addChildVC(vc: discover, title: "发现", image: UIImage(named: "tabbar_discover")!, selectImage: UIImage(named: "tabbar_discoverHL")!.withRenderingMode(.alwaysOriginal))
let I = IViewController()
self.addChildVC(vc: I, title: "我", image: #imageLiteral(resourceName: "tabbar_me.png"),selectImage: #imageLiteral(resourceName: "tabbar_meHL.png").withRenderingMode(.alwaysOriginal))
}
//1 控制器
//2 tabbar文字
//3 图片
//4 选中图片
func addChildVC(vc: UIViewController, title: String, image: UIImage, selectImage: UIImage) {
//设置导航控制器
let vcN = JNViewController(rootViewController: vc)
//把导航控制器设置为子控制器给tabbar管理
self.addChildViewController(vcN)
//设置文字、图片
vcN.tabBarItem = UITabBarItem(title: title, image: image, selectedImage: selectImage)
let color = UIColor.init(colorLiteralRed: 40.0 / 255.0, green: 177.0 / 255.0, blue: 26.0 / 255.0, alpha: 1.0)
//修改文字
let dic = [NSFontAttributeName: UIFont.systemFont(ofSize: 12),NSForegroundColorAttributeName: color]
vcN.tabBarItem.setTitleTextAttributes(dic, for: .selected)
let dic1 = [NSFontAttributeName: UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName: UIColor.lightGray]
vcN.tabBarItem.setTitleTextAttributes(dic1, for: .normal)
self.addChildViewController(vcN)
}
- 新建一个JNViewController.swift继承自UINavigationController,用来修改状态栏的属性:
override func viewDidLoad() {
super.viewDidLoad()
let color = UIColor.init(colorLiteralRed: 55.0 / 255.0, green: 53 / 255.0,blue: 60 / 255.0, alpha: 1.0)
self.navigationBar.barTintColor = color;
//修改状态栏的颜色
let dic = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20),NSForegroundColorAttributeName: UIColor.white]
self.navigationBar.titleTextAttributes = dic
}
可以把ViewController设置为微信下边的第一个,再分别新建微信的下边另外三个ViewController,分别为:ContactViewController.swift继承自UIViewController;DiscoverViewController.swift继承自UIViewController,DiscoverTableViewCell.swift继承自UITableViewCell;IViewController.swift继承自UIViewController,ITableViewCell.swift和I2TableViewCell.swift都继承自UITableViewCell;
-
在DiscoverViewController.swift中进行“发现”的设置:
- 先创建一个类
class CellMode : NSObject {
var title : String? = nil
var image : String? = nil
}
-
DiscoverViewController要继承UITableViewDelegate, UITableViewDataSource两个协议;
- 在DiscoverViewController中键入:
//放分组对应的数据
var arr : [[CellMode]] = [[CellMode]]()
var tableView : UITableView? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.title = "发现"
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.tableView = UITableView(frame: self.view.bounds, style: .grouped)
self.tableView?.delegate = self
self.tableView?.dataSource = self
self.view.addSubview(self.tableView!)
self.load()
//注册
self.tableView?.register(DiscoverTableViewCell.self, forCellReuseIdentifier: "tag")
}
func load() {
let mode1 = CellMode()
mode1.title = "朋友圈"
mode1.image = "ff_IconShowAlbum"
let arr = [mode1]
let mode2 = CellMode()
mode2.title = "扫一扫"
mode2.image = "ff_IconQRCode"
let mode3 = CellMode()
mode3.title = "摇一摇"
mode3.image = "ff_IconShake"
let arr1 = [mode2, mode3]
let mode4 = CellMode()
mode4.title = "附近的人"
mode4.image = "ff_IconLocationService"
let mode5 = CellMode()
mode5.title = "漂流瓶"
mode5.image = "ff_IconBottle"
let arr2 = [mode4, mode5]
let mode6 = CellMode()
mode6.title = "购物"
mode6.image = "CreditCard_ShoppingBag"
let mode7 = CellMode()
mode7.title = "游戏"
mode7.image = "Userguide_Gamecenter_icon"
let arr3 = [mode6, mode7]
self.arr.append(arr)
self.arr.append(arr1)
self.arr.append(arr2)
self.arr.append(arr3)
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.arr.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arr[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tag", for: indexPath) as! DiscoverTableViewCell
//取出模型
let m = self.arr[indexPath.section][indexPath.row]
//cell.imageV?.image = UIImage(named: m.image!)
//cell.titleLabel?.text = m.title
cell.model = m
//取消选中按钮
cell.selectionStyle = .none
//详情按钮
cell.accessoryType = .disclosureIndicator
return cell
}
//调整上页边距
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
}
//每个分组之间的间距
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
- 在DiscoverTableViewCell中键入:
private var imageV : UIImageView? = nil
private var titleLabel : UILabel? = nil
var model : CellMode {
set {
//newValue传进来默认值
self.imageV?.image = UIImage(named: newValue.image!)
self.titleLabel?.text = newValue.title
}
get {
return CellMode()
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.imageV = UIImageView(frame: CGRect(x: 15, y: 5, width: 30, height: 30))
self.addSubview(self.imageV!)
self.titleLabel = UILabel(frame: CGRect(x: 55, y: 5, width: 200, height: 30))
self.addSubview(self.titleLabel!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
}
- 在IViewController中新建一个类:
class CellMode1 : NSObject {
var image1 : String? = nil
var title1 : String? = nil
var title2 : String? = nil
var image2 : String? = nil
}
-
IViewController要继承UITableViewDelegate, UITableViewDataSource两个协议;
-
在IViewController中键入:
//放分组对应数据
var arr : [[CellMode1]] = [[CellMode1]]()
var tableView : UITableView? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
self.title = "我"
self.tableView = UITableView(frame: self.view.bounds, style: .grouped)
self.tableView?.delegate = self
self.tableView?.dataSource = self
self.view.addSubview(self.tableView!)
self.load()
//注册
self.tableView?.register(I2TableViewCell.self, forCellReuseIdentifier: "tag")
self.tableView?.register(ITableViewCell.self, forCellReuseIdentifier: "tag2")
}
func load() {
let mode0 = CellMode1()
mode0.image1 = "nv.jpg"
mode0.title1 = "大叔的小萝莉"
let arr0 = [mode0]
let mode1 = CellMode1()
mode1.title2 = "相册"
mode1.image2 = "MoreMyAlbum"
let mode2 = CellMode1()
mode2.title2 = "收藏"
mode2.image2 = "MoreMyFavorites"
let mode3 = CellMode1()
mode3.title2 = "钱包"
mode3.image2 = "MoreMyBankCard"
let mode4 = CellMode1()
mode4.title2 = "卡包"
mode4.image2 = "MyCardPackageIcon"
let arr1 = [mode1, mode2, mode3, mode4]
let mode5 = CellMode1()
mode5.title2 = "表情"
mode5.image2 = "MoreExpressionShops"
let arr2 = [mode5]
let mode6 = CellMode1()
mode6.title2 = "设置"
mode6.image2 = "MoreSetting"
let arr3 = [mode6]
self.arr.append(arr0)
self.arr.append(arr1)
self.arr.append(arr2)
self.arr.append(arr3)
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.arr.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.arr[section].count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tag", for: indexPath) as! I2TableViewCell
//取出模型
let m = self.arr[indexPath.section][indexPath.row]
//cell.imageH?.image = UIImage(named: m.image1!)
//cell.nameLabel?.text = m.title1
cell.mode2 = m
//取消选中按钮
cell.selectionStyle = .none
//详情按钮
cell.accessoryType = .disclosureIndicator
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "tag2", for: indexPath) as! ITableViewCell
let m = self.arr[indexPath.section][indexPath.row]
//cell.imageV?.image = UIImage(named: m.image2!)
//cell.titleLabel?.text = m.title2
cell.mode1 = m
//取消选中按钮
cell.selectionStyle = .none
//详情按钮
cell.accessoryType = .disclosureIndicator
return cell
}
//cell.model = m
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 100
} else {
return 50
}
}
//调整上页边距
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0.1
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
}
//每个分组之间的间距
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
- 在ITableViewCell中键入:
var imageV : UIImageView? = nil
var titleLabel : UILabel? = nil
var mode1 : CellMode1 {
set {
self.imageV?.image = UIImage(named: newValue.image2!)
self.titleLabel?.text = newValue.title2
}
get {
return CellMode1()
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.imageV = UIImageView(frame: CGRect(x: 15, y: 5, width: 30, height: 30))
self.addSubview(self.imageV!)
self.titleLabel = UILabel(frame: CGRect(x: 55, y: 5, width: 200, height: 30))
self.addSubview(self.titleLabel!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
- 在I2TableViewCell中键入:
var imageH : UIImageView? = nil
var nameLabel : UILabel? = nil
var mode2 : CellMode1 {
set {
self.imageH?.image = UIImage(named: newValue.image1!)
self.nameLabel?.text = newValue.title1
}
get {
return CellMode1()
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.imageH = UIImageView(frame: CGRect(x: 15, y: 5, width: 80, height: 80))
self.imageH?.layer.cornerRadius = 8
self.imageH?.layer.masksToBounds = true
self.addSubview(self.imageH!)
self.nameLabel = UILabel(frame: CGRect(x: 105, y: 5, width: 200, height: 40))
self.nameLabel?.font = UIFont.systemFont(ofSize: 20)
self.addSubview(self.nameLabel!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
- 在ViewController中对背景颜色进行设置:
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
- 运行结果:
day16
新闻页面
-
新建三个UIViewCell,分别为:TableViewCell,STableViewCell,TTableViewCell,并分别创建xib文件:
- 分别在这几个xib中进行控件的拖动及约束的设置,三个图如下所示:
-
把相应的控件按住control拖动到相应的TableViewCell中设置为全局变量;
-
ViewController继承UITableViewDelegate, UITableViewDataSource协议;
-
向ViewController中键入:
var tabelview : UITableView! = nil
override func viewDidLoad() {
super.viewDidLoad()
self.tabelview = UITableView(frame: self.view.frame, style: .grouped)
self.tabelview.delegate = self
self.tabelview.dataSource = self
self.view.addSubview(self.tabelview)
//注册
self.tabelview.register(UINib(nibName: "TableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "tag")
self.tabelview.register(UINib(nibName: "STableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "tag1")
self.tabelview.register(UINib(nibName: "TTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "tag2")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "tag", for: indexPath) as! TableViewCell
cell.titlelabel.text = "摩尔金融"
cell.detaillabel.text = "2016年投资展望:大势分析+10大机会+10大金股"
cell.imageview.image = UIImage(named: "1")
cell.selectionStyle = .none
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "tag1", for: indexPath) as! STableViewCell
cell.nameLabel.text = "做封面人物 秀出我的态度"
cell.firstimage.image = UIImage(named: "2")
cell.secondimage.image = UIImage(named: "3")
cell.thirdimage.image = UIImage(named: "4")
cell.selectionStyle = .none
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "tag2", for: indexPath) as! TTableViewCell
cell.titleL.text = "新闻资讯"
cell.image1.image = UIImage(named: "5")
cell.selectionStyle = .none
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
} else if indexPath.row == 1 {
return 150
} else {
return 200
}
}
- 运行结果:
day17
运用Main.storyboard
- 打开Main.storyboard,将第一个ViewController设置为Navigation Controller,再拖动两个View Controller,第一个View Controller上拖一个button控件,按住control键将button连到第二个View Controller上,选择show,结果如图:
- 新建一个RedViewController继承自UIViewController:
var name : String! = nil
override func viewDidLoad() {
super.viewDidLoad()
print(name)
}
- 在ViewController中键入:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//获取目标控制器
let vc = segue.destination;
//判断目标控制器是不是你想跳转的
if vc is RedViewController {
let descriptionVc = vc as! RedViewController
descriptionVc.name = "张三"
}
}
- 运行结果:
单元格
- 在Main.storyboard中进行控件的拖动及约束的设置,如图:
-
新建RootTableViewCell继承自UITableViewCell,将刚才约束好的控件进行拖动至RootTableViewCell中;
-
将tableView按住control拖动至ViewController;
-
在ViewController中键入:
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RootTableViewCell", for: indexPath) as! RootTableViewCell
cell.titleLabel.text = "张姗"
cell.headImage.image = UIImage(named: "nv.jpg")
return cell
}
- 运行结果:
电话本
- 在Main.storyboard中进行如前面的设置,三个页面,并进行约束,结果如图:
-
新建RootTableViewCell继承自UITableViewCell,将之前设置好的控件headImage
,titleLabel,detailLabel按住control拖入NextViewController中; -
新建NextViewController继承自UIViewController,将之前设置好的控件nameTextField
,phoneTextField按住control拖入NextViewController中并键入:
var name : String! = nil
var phone : String! = nil
var sendMsg:((String, String)-> Void)! = nil
override func viewDidLoad() {
super.viewDidLoad()
self.nameTextField.text = self.name
self.phoneTextField.text = self.phone
}
@IBAction func btnAction(_ sender: UIButton) {
self.sendMsg(self.nameTextField.text!, self.phoneTextField.text!)
let _ = self.navigationController?.popViewController(animated: true)
}
- 在ViewController中拖入tableView并键入:
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "RootTableViewCell", for: indexPath) as! RootTableViewCell
cell.headImage.image = UIImage(named: "nv.jpg")
cell.titleLabel.text = "姓名"
cell.detailLabel.text = "电话"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let _ = self.RootTableView .indexPath(for: sender as! RootTableViewCell)
//先控制第二个控制器
let second = segue.destination
if second is NextViewController {
let sec = second as! NextViewController
sec.name = "张三"
sec.phone = "3333333333"
sec.sendMsg = {
print("name = \($0), phone = \($1)")
}
}
}
// func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
// return 0.1
// }
- 运行结果:
练习
- 在Main.storyboard中进行控件的拖动及约束的设置,如图:
-
将设置好的控件按住control拖入ViewController;
-
在viewcontroller中添加两个方法:
//点击屏幕时走
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.textfield.resignFirstResponder()
}
//结束编辑
func textFieldDidEndEditing(_ textField: UITextField) {
self.label1.text = self.textfield.text
}
- 运行结果:
day18
好友列表
- 在ViewController中新建一个枚举:
//控制分组的开关枚举
enum Switch {
case open
case close
}
- 在ViewController中新建一个手势类:
//继承单击手势
class Tap : UITapGestureRecognizer {
var section : Int? = nil
}
-
ViewController继承自UITableViewDelegate, UITableViewDataSource协议:
-
在ViewController中键入:
var tableView : UITableView! = nil
//前面是分组的头部,后面是分组中的人员
var dic : [String:[String]] = [String:[String]]()
//keys的数组
var arr : [String] = [String]()
var SwitchArr : [Switch] = [.close,.close,.close]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
dic["我的好友🤓️"] = ["张三","李四","王五","赵六","小白"]
dic["朋友🤗️"] = ["老李","小六","帅帅","张姗"]
dic["同学🤒️"] = ["琼哥","一姐","小哥","师俊南","靖媛","猫咪"]
//初始化一个数组
for item in dic.keys {
arr.append(item)
}
self.tableView = UITableView(frame: self.view.frame, style: .grouped)
self.tableView.delegate = self
self.tableView.dataSource = self
self.view.addSubview(self.tableView)
}
//头部视图
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
//自定义头部视图
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 50))
//单击手势
let tap = Tap()
tap.section = section
view.addGestureRecognizer(tap)
tap.addTarget(self, action: #selector(tapAction(sender:)))
let label = UILabel(frame: CGRect(x: 300, y: 20, width: 120, height: 40))
if self.SwitchArr[section] == .close {
label.text = "▼"
} else {
label.text = "▲"
}
view.addSubview(label)
let label1 = UILabel(frame: CGRect(x: 20, y: 20, width: 120, height: 40))
label1.text = self.arr[section]
view.addSubview(label1)
return view
}
//打开手势方法
func tapAction(sender: Tap) {
//先取出来section
let temp = sender.section //当前点击第几个
//取反
if self.SwitchArr[temp!] == .close {
self.SwitchArr[temp!] = .open
} else {
self.SwitchArr[temp!] = .close
}
//刷新表格
self.tableView.reloadData()
}
//尾部视图
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0.1
}
//多少分组
func numberOfSections(in tableView: UITableView) -> Int {
return self.arr.count
}
//每组多少个
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let Sswitch = self.SwitchArr[section]
if Sswitch == .close {
return 0
}
//先从数组中取出keys,再根据keys值取出对应的数组
let array = dic[self.arr[section]]
return (array?.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "tag")
let array = dic[self.arr[indexPath.section]]
let str = array?[indexPath.row]
cell.textLabel?.text = str
return cell
}
- 运行结果:
(注:由于这周学的都是向xib中拖动控件而不是打代码,所以详细的拖动步骤以及约束的设置都没有向大家详细的介绍,约束不会的地方以后会为大家再进行总结。)
网友评论