APP.SWIFT
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
varwindow:UIWindow?
funcapplication(_application:UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:Any]?) ->Bool{
// Override point for customization after application launch.
//底部标签控制器
letoneNav =UINavigationController(rootViewController:OneVC())
oneNav.tabBarItem=UITabBarItem(title:"首页", image:UIImage(named:"dongTai"), selectedImage:UIImage(named:"dongTai_H"))
lettwoNav =UINavigationController(rootViewController:VCTwo())
twoNav.tabBarItem=UITabBarItem(title:"微淘", image:UIImage(named:"dongTai"), selectedImage:UIImage(named:"dongTai_H"))
letthreeNav =UINavigationController(rootViewController:ThreeVC())
threeNav.tabBarItem=UITabBarItem(title:"消息", image:UIImage(named:"dongTai"), selectedImage:UIImage(named:"dongTai_H"))
letfourNav =UINavigationController(rootViewController:FourVC())
fourNav.tabBarItem=UITabBarItem(title:"购物车", image:UIImage(named:"dongTai"), selectedImage:UIImage(named:"dongTai_H"))
letfiveNav =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
return true
}
funcapplicationWillResignActive(_application:UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
funcapplicationDidEnterBackground(_application:UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
funcapplicationWillEnterForeground(_application:UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
funcapplicationDidBecomeActive(_application:UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
funcapplicationWillTerminate(_application:UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
VCTwo.swift
//分段控制器
import UIKit
class VCTwo: UIViewController {
privatelazyvarpageTitleView: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)
letpageTitleView =MFPageTitleView(frame:CGRect(x: 0, y:navHeight, width:SCREEN_WIDTH, height: 41), titles: ["全部","上新","视频直播","特别关注","达人"], config: config)
pageTitleView.pageTitleViewDelegate=self
returnpageTitleView
}()
privatelazyvarpageContentView:MFPageContentView= {
varchildControllers = [UIViewController]()
forkin0..<5 {
varvc =UIViewController()
letred:CGFloat=CGFloat(arc4random() % 256)
letgreen:CGFloat=CGFloat(arc4random() % 256)
letblue:CGFloat=CGFloat(arc4random() % 256)
vc.view.backgroundColor=UIColor(red: red / 255.0, green: green / 255.0, blue: blue / 255.0, alpha: 1.0)
ifk==4{
vc =WeiTaoVC()
}
childControllers.append(vc)
}
letpageContentViewY =pageTitleView.frame.maxY
letpageContentView =MFPageContentView(frame:CGRect(x: 0, y: pageContentViewY, width:SCREEN_WIDTH, height:SCREEN_HEIGHT-pageContentViewY), parentVC:self, childVCs: childControllers)
pageContentView.pageContentViewDelegate=self
returnpageContentView
}()
overridefuncviewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
view.addSubview(pageTitleView)
view.addSubview(pageContentView)
}
}
extension VCTwo: MFPageTitleViewDelegate, MFPageContentViewDelegate {
funcselectedIndexInPageTitleView(pageTitleView:MFPageTitleView, selectedIndex:Int) {
self.pageContentView.setPageContentViewCurrentIndex(currentIndex: selectedIndex)
}
funcpageContentViewScroll(progress:CGFloat, originalIndex:Int, targetIndex:Int) {
self.pageTitleView.setPageTitleView(progress: progress, originalIndex: originalIndex, targetIndex: targetIndex)
}
}
WeiTaoVC.swift
import UIKit
class WeiTaoVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
letmArr:NSMutableArray= []
varcellHight:CGFloat= 0
var tv:UITableView?
overridefuncviewDidLoad() {
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
letpath =Bundle.main.path(forResource:"daren", ofType:"json")
leturl =URL(fileURLWithPath: path!)
do{
letdata =tryData(contentsOf: url)
let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
// print(jsonData)
letjsonArr:NSArray= jsonDataas!NSArray
print(jsonArr)
foriteminjsonArr {
letk:NSDictionary= itemas!NSDictionary
ifk.object(forKey:"type")as!String=="0"{
letmd: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)
}elseifk.object(forKey:"type")as!String=="1"{
letmd: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{
letmd: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)
}
}
}catchleterrorasError? {
print(errorasAny)
}
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()
}
functableView(_tableView:UITableView, numberOfRowsInSection section:Int) ->Int{
returnmArr.count
}
functableView(_tableView:UITableView, cellForRowAt indexPath:IndexPath) ->UITableViewCell{
letitem =mArr[indexPath.row]
ifitemisOneMD{
letmd:OneMD=mArr[indexPath.row]as!OneMD
letcell:OneCell= tableView.dequeueReusableCell(withIdentifier:"cell1")as!OneCell
cell.setCellWithData(model: md)
returncell
}elseifitemisTwoMD{
letmd:TwoMD=mArr[indexPath.row]as!TwoMD
letcell:TwoCell= tableView.dequeueReusableCell(withIdentifier:"cell2")as!TwoCell
cell.setCellWithData(model: md)
print(cellHight)
returncell
}else{
letmd:ThreeMD=mArr[indexPath.row]as!ThreeMD
letcell:ThreeCell= tableView.dequeueReusableCell(withIdentifier:"cell3")as!ThreeCell
cell.setCellWithData(model: md)
returncell
}
}
functableView(_tableView:UITableView, heightForRowAt indexPath:IndexPath) ->CGFloat{
letitem =mArr[indexPath.row]
ifitemisTwoMD{
returncellHight+10
}else{
return200
}
}
@objcfunctest(nofi :Notification){
cellHight = nofi.userInfo!["post"] as! CGFloat
tv?.reloadData()
/// 移除通知
NotificationCenter.default.removeObserver(self)
}
}
viewcontroller
import UIKit
classViewController:UIViewController{
overridefuncviewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
letpath =Bundle.main.path(forResource:"daren", ofType:"json")
leturl =URL(fileURLWithPath: path!)
do{
letdata =tryData(contentsOf: url)
let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
// print(jsonData)
letjsonDic:NSArray= jsonDataas!NSArray
print(jsonDic)
}catchleterrorasError? {
print(errorasAny)
}
}
}
OneMD
import UIKit
classOneMD:NSObject{
varheadImg:String
vartitle:String
vardetailTitle:String
varimg:String
vartime:String
varnum:String
vartype: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
}
}
TwoMD
import UIKit
classOneMD:NSObject{
varheadImg:String
vartitle:String
vardetailTitle:String
varimg:String
vartime:String
varnum:String
vartype: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
}
}
ThreeMD
import UIKit
classThreeMD:NSObject{
varheadImg:String
vartitle:String
vardetailTitle:String
varimg:String
vartime:String
varnum:String
vartype: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
}
}
oneCell
import UIKit
class OneCell: UITableViewCell {
@IBOutletweakvarheadImgView:UIImageView!
@IBOutletweakvartitleLabel:UILabel!
@IBOutletweakvartimeLabel:UILabel!
@IBOutletweakvardetailLabel:UILabel!
@IBOutletweakvarimgView:UIImageView!
@IBOutletweakvarnumLabel:UILabel!
@IBOutletweakvarlayoutContentHight:NSLayoutConstraint!
funcsetCellWithData(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
}
overridefuncawakeFromNib() {
super.awakeFromNib()
// Initialization code
}
overridefuncsetSelected(_selected:Bool, animated:Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
import UIKit
class TwoCell: UITableViewCell {
@IBOutletweakvarheadImgView:UIImageView!
@IBOutletweakvartitleLabel:UILabel!
@IBOutletweakvartimeLabel:UILabel!
@IBOutletweakvardetailLabel:UILabel!
@IBOutletweakvarpicView:UIView!
@IBOutletweakvarnumLabel:UILabel!
@IBOutletweakvarlayoutContentHight:NSLayoutConstraint!
@IBOutletweakvarlayoutPicHight:NSLayoutConstraint!
vararr:NSArray= []
varHight:CGFloat= 0
funcsetCellWithData(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: 高度
funcautoLabelHeight(with text:String, labelWidth:CGFloat,attributes : [NSAttributedString.Key:Any]) ->CGFloat{
varsize =CGRect()
letsize2 =CGSize(width: labelWidth, height: 0)//设置label的最大宽度
size = text.boundingRect(with: size2, options:NSStringDrawingOptions.usesLineFragmentOrigin, attributes:nil, context:nil);
returnsize.size.height
}
overridefunclayoutSubviews() {
let labHight:CGFloat = self.autoLabelHeight(with: self.detailLabel.text!, labelWidth: self.frame.width-20, attributes: [NSAttributedString.Key.font: self.detailLabel.font])
print(labHight)
letimgWidth:CGFloat= (self.frame.width-20-12)/3.0
print(imgWidth)
letcolum:Int= 3
vark:Int= 0
foriteminarr{
letimgView: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: itemas!String)
k+=1
}
lethightImg: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])
}
overridefuncawakeFromNib() {
super.awakeFromNib()
// Initialization code
}
overridefuncsetSelected(_selected:Bool, animated:Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
ThreeCell
import UIKit
class ThreeCell: UITableViewCell {
@IBOutletweakvarheadImgView:UIImageView!
@IBOutletweakvartitleLabel:UILabel!
@IBOutletweakvartimeLabel:UILabel!
@IBOutletweakvardetailLabel:UILabel!
@IBOutletweakvarimgView:UIImageView!
@IBOutletweakvarnumLabel:UILabel!
funcsetCellWithData(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
}
overridefuncawakeFromNib() {
super.awakeFromNib()
// Initialization code
}
overridefuncsetSelected(_selected:Bool, animated:Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}