swift 2

来源:互联网 发布:echo动作数据mmd 编辑:程序博客网 时间:2024/06/06 00:32

json 初级解析

已知返回数据格式

var imagearr :NSMutableArray!

var urlarr :NSMutableArray!



{    "state": true,     "body": [        {            "cover": "/images/push/201611/20161109093112_36.jpg",             "id": 1,             "type": 0,             "content": "http://..............."        },         {            "cover": "/images/push/201611/20161109093127_313.jpg",             "id": 2,             "type": 0,             "content": "http:/..............."        },         {            "cover": "/images/push/201611/20161109093141_286.jpg",             "id": 3,             "type": 0,             "content": "http://..................."        }    ]}

    func addData(){

        let commen =comment()

        imagearr =NSMutableArray()

        urlarr =NSMutableArray()

// 调用oc的请求数据的方法 AnyObject是返回的json数据 如上 在block中进行解析

        commen.getScrollViewPhotos { (AnyObject)in

            let body = AnyObjectas?NSDictionary

            let bodyArr = body?["body"]as?NSArray

            for dicin bodyArr! {

                let bodydic = dicas?NSDictionary

                let imagestr = bodydic?["cover"]as!String

                let urlstr = bodydic?["content"]as!String

                imagearr.add(URLADDRESS+ imagestr)

                urlarr.add(urlstr)

            }

         print(imagearr)

        }

    }

json 解析成model

model 文件是oc 文件 swift 进行调用

commen.getShouYeLanMu{(AnyObject)in

            let body = AnyObjectas?NSDictionary

            let bodyArr = body?["body"]as?NSArray

            for dicin bodyArr! {

// 把字典转换成model的方法

               let model =ShouYeLanMuModel(dic: dicas! [NSObject :AnyObject])

               shouyelanmuArr.append(model!)

            }

 

        print(shouyelanmuArr)

        }



设置导航栏的标题和返回按钮的颜色

self.navigationController?.navigationBar.isTranslucent = false // 导航栏不透明

self.navigationController?.navigationBar.tintColor = UIColor.orange // 返回按钮颜色

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black]// 标题的颜色

创建右按钮

self.navigationItem.rightBarButtonItem =UIBarButtonItem(image:UIImage(named:"searchbutton"), style: .plain, target: self, action:#selector(searchAction))


    func searchAction(){

        let search =SearchViewController()

        search.hidesBottomBarWhenPushed =true

        self.navigationController?.pushViewController(search, animated: true)

    }

collectionview 创建

 let layout =UICollectionViewFlowLayout()
        layout.
itemSize =CGSize(width:WIDTH/4.0,height:WIDTH/4.0 + 5*FITWIDTH)
        layout.
minimumLineSpacing =0
        layout.
minimumInteritemSpacing =0
       
headerCollectionView =UICollectionView(frame:CGRect(x:0,y:WIDTH*9/16,width:WIDTH,height:200),collectionViewLayout:layout)
       
headerCollectionView.delegate =self
       
headerCollectionView.dataSource =self
       
headerCollectionView.backgroundColor =UIColor.white
       
headerCollectionView.register(HeadTitleCollectionCell.self, forCellWithReuseIdentifier: “titleReuse”)

func numberOfSections(in collectionView:UICollectionView) ->Int {
       
return1
    }
   
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section:Int) ->Int {
       
return8
    }
   
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath:IndexPath) ->UICollectionViewCell {
       
let titlecell = collectionView.dequeueReusableCell(withReuseIdentifier:"titleReuse", for: indexPath)
       
       
return titlecell
    }
cell 的创建
class BaseCollectionViewCell:UICollectionViewCell {
   
overrideinit(frame:CGRect){
   
super.init(frame: frame)
   
self.createView()
    }
   
func createView(){
   
// 创建视图
    }
   
requiredinit(coder aDecoder:NSCoder) {
       
fatalError("init(coder:) has not been implemented")
    }
}




0 0