1.导入第三方PageTitleView 2.在AppDelegate.swift中:
//底部标签控制器
let oneNav = UINavigationController(rootViewController: OneVC())
oneNav.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
let twoNav = UINavigationController(rootViewController: VCTwo())
twoNav.tabBarItem = UITabBarItem(title: "微淘", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
let threeNav = UINavigationController(rootViewController: ThreeVC())
threeNav.tabBarItem = UITabBarItem(title: "消息", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
let fourNav = UINavigationController(rootViewController: FourVC())
fourNav.tabBarItem = UITabBarItem(title: "购物车", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
let fiveNav = UINavigationController(rootViewController: FiveVC())
fiveNav.tabBarItem = UITabBarItem(title: "我的", image: UIImage(named: "dongTai"), selectedImage: UIImage(named: "dongTai_H"))
let tbv = UITabBarController()
tbv.viewControllers = [oneNav,twoNav,threeNav,fourNav,fiveNav]
tbv.selectedViewController = twoNav
window?.rootViewController = tbv
3.在ViewController.swift中:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let path = Bundle.main.path(forResource: "daren", ofType: "json")
let url = URL(fileURLWithPath: path!)
do {
let data = try Data(contentsOf: url)
let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
// print(jsonData)
let jsonDic:NSArray = jsonData as! NSArray
print(jsonDic)
} catch let error as Error? {
print(error as Any)
}
}
} 4.OneVC、ThreeVC、FourVC、FiveVC继承于UIViewController 5.创建VCTwo继承于UIViewController
代码:
import UIKit
class VCTwo: UIViewController {
private lazy var pageTitleView: MFPageTitleView = {
let config = MFPageTitleViewConfig()
config.titleColor = colorWithRGB(r: 43, g: 43, b: 43)
config.titleSelectedColor = colorWithRGB(r: 211, g: 0, b: 0)
config.titleFont = UIFont.systemFont(ofSize: 14, weight: .regular)
config.indicatorColor = colorWithRGB(r: 211, g: 0, b: 0)
let pageTitleView = MFPageTitleView(frame: CGRect(x: 0, y: navHeight, width: SCREEN_WIDTH, height: 41), titles: ["全部", "上新", "视频直播", "特别关注", "达人"], config: config)
pageTitleView.pageTitleViewDelegate = self
return pageTitleView
}()
private lazy var pageContentView: MFPageContentView = {
var childControllers = [UIViewController]()
for k in 0..<5 {
var vc = UIViewController()
let red: CGFloat = CGFloat(arc4random() % 256)
let green: CGFloat = CGFloat(arc4random() % 256)
let blue: CGFloat = CGFloat(arc4random() % 256)
vc.view.backgroundColor = UIColor(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: 1.0)
if k==4{
vc = WeiTaoVC()
}
childControllers.append(vc)
}
let pageContentViewY = pageTitleView.frame.maxY
let pageContentView = MFPageContentView(frame: CGRect(x: 0, y: pageContentViewY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-pageContentViewY), parentVC: self, childVCs: childControllers)
pageContentView.pageContentViewDelegate = self
return pageContentView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(pageTitleView)
view.addSubview(pageContentView)
}
}
extension VCTwo: MFPageTitleViewDelegate, MFPageContentViewDelegate {
func selectedIndexInPageTitleView(pageTitleView: MFPageTitleView, selectedIndex: Int) {
self.pageContentView.setPageContentViewCurrentIndex(currentIndex: selectedIndex)
}
func pageContentViewScroll(progress: CGFloat, originalIndex: Int, targetIndex: Int) {
self.pageTitleView.setPageTitleView(progress: progress, originalIndex: originalIndex, targetIndex: targetIndex)
}
} 6.创建WeiTaoVC继承于UIViewController
代码:
import UIKit
class WeiTaoVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
let mArr:NSMutableArray = []
var cellHight:CGFloat = 0
var tv:UITableView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
NotificationCenter.default.addObserver(self, selector: #selector(test), name: NSNotification.Name(rawValue:"cellHight"), object: nil)
view.backgroundColor = UIColor.white
let path = Bundle.main.path(forResource: "daren", ofType: "json")
let url = URL(fileURLWithPath: path!)
do {
let data = try Data(contentsOf: url)
let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
// print(jsonData)
let jsonArr:NSArray = jsonData as! NSArray
print(jsonArr)
for item in jsonArr {
let k:NSDictionary = item as! NSDictionary
if k.object(forKey: "type") as! String == "0"{
let md:OneMD = OneMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
mArr.add(md)
}else if k.object(forKey: "type") as! String == "1" {
let md:TwoMD = TwoMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! NSMutableArray, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
mArr.add(md)
}else{
let md:ThreeMD = ThreeMD(headImg: k.object(forKey: "head") as! String, title: k.object(forKey: "title") as! String, detailTitle: k.object(forKey: "detailTitle") as! String, img: k.object(forKey: "img") as! String, time: k.object(forKey: "time") as! String, num: k.object(forKey: "num") as! String, type: k.object(forKey: "type") as! String)
mArr.add(md)
}
}
} catch let error as Error? {
print(error as Any)
}
tv = UITableView(frame:CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height-120), style: .plain)
view.addSubview(tv!)
tv!.delegate = self
tv!.dataSource = self
tv!.register(UINib(nibName: "OneCell", bundle: Bundle.main), forCellReuseIdentifier: "cell1")
tv?.register(UINib(nibName: "TwoCell", bundle: Bundle.main), forCellReuseIdentifier: "cell2")
tv!.register(UINib(nibName: "ThreeCell", bundle: Bundle.main), forCellReuseIdentifier: "cell3")
tv!.tableFooterView = UIView()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mArr.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let item = mArr[indexPath.row]
if item is OneMD{
let md:OneMD = mArr[indexPath.row] as! OneMD
let cell:OneCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! OneCell
cell.setCellWithData(model: md)
return cell
} else if item is TwoMD{
let md:TwoMD = mArr[indexPath.row] as! TwoMD
let cell:TwoCell = tableView.dequeueReusableCell(withIdentifier: "cell2") as! TwoCell
cell.setCellWithData(model: md)
print(cellHight)
return cell
}else{
let md:ThreeMD = mArr[indexPath.row] as! ThreeMD
let cell:ThreeCell = tableView.dequeueReusableCell(withIdentifier: "cell3") as! ThreeCell
cell.setCellWithData(model: md)
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let item = mArr[indexPath.row]
if item is TwoMD{
return cellHight+10
} else{
return 200
}
}
@objc func test(nofi : Notification){
cellHight = nofi.userInfo!["post"] as! CGFloat
tv?.reloadData()
/// 移除通知
NotificationCenter.default.removeObserver(self)
}
} 7.创建OneMD继承于NSObject
代码:import UIKit
class OneMD: NSObject {
var headImg:String
var title:String
var detailTitle:String
var img:String
var time:String
var num:String
var type:String
init(headImg:String,title:String,detailTitle:String,img:String,time:String,num:String,type:String) {
self.headImg = headImg
self.title = title
self.time = time
self.detailTitle = detailTitle
self.img = img
self.num = num
self.type = type
}
} 8.创建TwoMD继承于NSObject
代码:
import UIKit
class TwoMD: NSObject {
var headImg:String
var title:String
var detailTitle:String
var img:NSMutableArray
var time:String
var num:String
var type:String
init(headImg:String,title:String,detailTitle:String,img:NSMutableArray,time:String,num:String,type:String) {
self.headImg = headImg
self.title = title
self.time = time
self.detailTitle = detailTitle
self.img = img
self.num = num
self.type = type
}
} 9.创建ThreeMD继承于NSObject
代码:
import UIKit
class ThreeMD: NSObject {
var headImg:String
var title:String
var detailTitle:String
var img:String
var time:String
var num:String
var type:String
init(headImg:String,title:String,detailTitle:String,img:String,time:String,num:String,type:String) {
self.headImg = headImg
self.title = title
self.time = time
self.detailTitle = detailTitle
self.img = img
self.num = num
self.type = type
}
} 10.创建OneCell继承于UITableViewCell,选中Also create file
代码:
import UIKit
class OneCell: UITableViewCell {
@IBOutlet weak var headImgView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var numLabel: UILabel!
@IBOutlet weak var layoutContentHight: NSLayoutConstraint!
func setCellWithData(model:OneMD) {
self.headImgView.image = UIImage(named: model.headImg)
self.titleLabel.text = model.title
self.timeLabel.text = model.time
self.detailLabel.text = model.detailTitle
self.imgView.image = UIImage(named: model.img)
self.numLabel.text = model.num
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
} 11.创建TwoCell继承于UITableViewCell,选中Also create file
代码:
import UIKit
class TwoCell: UITableViewCell {
@IBOutlet weak var headImgView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var picView: UIView!
@IBOutlet weak var numLabel: UILabel!
@IBOutlet weak var layoutContentHight: NSLayoutConstraint!
@IBOutlet weak var layoutPicHight: NSLayoutConstraint!
var arr:NSArray = []
var Hight:CGFloat = 0
func setCellWithData(model:TwoMD) {
self.headImgView.image = UIImage(named: model.headImg)
self.titleLabel.text = model.title
self.timeLabel.text = model.time
self.detailLabel.text = model.detailTitle
self.numLabel.text = model.num
self.detailLabel.font=UIFont.systemFont(ofSize: 15)
arr = model.img
}
///label高度自适应
/// - Parameters:
/// - text: 文字
/// - labelWidth: 最大宽度
/// - attributes: 字体,行距等
/// - Returns: 高度
func autoLabelHeight(with text:String , labelWidth: CGFloat ,attributes : [NSAttributedString.Key : Any]) -> CGFloat{
var size = CGRect()
let size2 = CGSize(width: labelWidth, height: 0)//设置label的最大宽度
size = text.boundingRect(with: size2, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes:nil, context: nil);
return size.size.height
}
override func layoutSubviews() {
let labHight:CGFloat = self.autoLabelHeight(with: self.detailLabel.text!, labelWidth: self.frame.width-20, attributes: [NSAttributedString.Key.font: self.detailLabel.font])
print(labHight)
let imgWidth:CGFloat = (self.frame.width-20-12)/3.0
print(imgWidth)
let colum:Int = 3
var k:Int = 0
for item in arr {
let imgView:UIImageView = UIImageView(frame: CGRect(x: CGFloat(k%colum)*(imgWidth+5), y:CGFloat(k/colum)*(imgWidth+5), width: imgWidth, height: imgWidth))
self.picView.addSubview(imgView)
imgView.image = UIImage(named: item as!String)
k+=1
}
let hightImg:CGFloat = CGFloat(arr.count/colum+1)*(imgWidth+5)
self.layoutPicHight.constant = hightImg
self.layoutContentHight.constant = labHight+95+self.layoutPicHight.constant+25
Hight = self.layoutContentHight.constant
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "cellHight"), object: self, userInfo: ["post":Hight])
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
12.创建ThreeCell继承于UITableViewCell,选中Also create file
代码:
import UIKit
class ThreeCell: UITableViewCell {
@IBOutlet weak var headImgView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
@IBOutlet weak var detailLabel: UILabel!
@IBOutlet weak var imgView: UIImageView!
@IBOutlet weak var numLabel: UILabel!
func setCellWithData(model:ThreeMD) {
self.headImgView.image = UIImage(named: model.headImg)
self.titleLabel.text = model.title
self.timeLabel.text = model.time
self.detailLabel.text = model.detailTitle
self.imgView.image = UIImage(named: model.img)
self.numLabel.text = model.num
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
|
请发表评论