例系

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

    }


}


TWOcell

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

    }


}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容

  • /** * 拦截器(请求之前调用) * @fn: 请求函数 */ function wxPromisify(fn)...
    孤单的小草阅读 784评论 0 0
  • 今天是什么日子 今日腊月十三,离过年越来越近了 坚持早起第18天,已经两周,争取21天养成好习惯 起床:6:50(...
    如月公子阅读 235评论 0 1
  • 文/余森 夜幕下的校园,总是格外吸引人的,和朋友在荫间小道上,嬉笑打闹着,说不出的轻松,真的很喜欢这样平静没有波澜...
    余I森阅读 726评论 10 18
  • 1.1 引言 持续集成的价值是什么?对于开发和测试人员又意味着什么呢? 1.2 概念 “持续集成”一词来源与极限编...
    苦叶子阅读 1,548评论 0 11
  • 在皖北亳州,谁家早餐盘里没有几朵“黄云”呢? 每到清晨,全程飘着同一种香味,那就是牛肉馍的香。牛肉馍,因为,双面被...
    丹崖阅读 1,731评论 0 1