Swift学习记录 Project 6

从100 Days of Swift中学习,实践

目前正在学习swift , 刚刚接触了解了一部分语法后就因为自己在OC上使用reactiveOBJC还算熟练,想直接学会rxswift和reactiveswift ,中间因为xcode有时候索引失效和一些其他原因,想过放弃学习,无意中看到 关于iOS学习进阶的必读一些博客总结 这个文章时看到了 100 Days of Swift, 感觉从一次次项目中,更加能够坚实我的基础, 所以决定从基础开始,跟着一步步往前走


Project 6

  • 学会使用collectionview
  • 学会使用BarButtonItem 自定义导航栏
  • 动态计算label高度,控制scrollerview的contentsize
  • 学会使用UIAlertViewController 来展示actionsheet

UICollectionView 的使用

因为我是使用storyboard构建的UI,所以在storyboard里设置好约束,delegate,dataSource,ViewController里面实现代理方法,自定义一个cell 就可以算是完成了整个UICollectionView的构建

let photocellsId = "photoscells"
//注册cell  
PhotosCollectionView.register(ImageCell.classForCoder(), forCellWithReuseIdentifier: photocellsId)

//获取数据源
let data = photosData().data

extension PhotosViewController:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photocellsId, for: indexPath) as! ImageCell
        let model:[String:Any] = data[indexPath.row] as! [String : Any]
        cell.photoImageView.image = UIImage.init(named: model["image"] as! String)
        return cell
    }   
}

extension PhotosViewController:UICollectionViewDelegateFlowLayout,UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

//用于需要计算图片尺寸时的操作
//        let model:[String:Any] = data[indexPath.row] as! [String : Any]
//        let size = UIImage.init(named: model["image"] as! String)!.size
//
//        let backsize = CGSize.init(width: (collectionView.frame.width)/3, height: size.height/(size.width/((collectionView.frame.width)/3)))
//        return backsize

//返回固定尺寸
        return CGSize.init(width:50, height: 50)
    }
   
//修改上左下右边距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(0, 0, 0, 0)
    }    
}

在整个项目里面,cells的尺寸都是固定的, 那就不需要去做成瀑布流的模式,网上很多关于瀑布流的文章,可以自行翻阅。

UIAlertViewController

UIAlertViewController 支持两种style(alert,actionsheet)。

let alertAction = UIAlertController.init(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
        let cancel = UIAlertAction.init(title: "cancel", style: UIAlertActionStyle.cancel) { (_) in

        }

        let action0 = UIAlertAction.init(title: "Copy Link", style: UIAlertActionStyle.default) { (_) in

        }
        let action1 =  UIAlertAction.init(title: "Save Image", style: UIAlertActionStyle.default) { (_) in

        }
        let action2 =  UIAlertAction.init(title: "Share", style: UIAlertActionStyle.default) { (_) in

        }
        let action3 =  UIAlertAction.init(title: "Report", style: UIAlertActionStyle.default) { (_) in

        }
        alertAction.addAction(cancel)
        alertAction.addAction(action3)
        alertAction.addAction(action2)
        alertAction.addAction(action1)
        alertAction.addAction(action0)
        self.present(alertAction, animated: true, completion: nil)

动态计算label高度之前有写过,就不重复了。


100 Days of Swift 跟着他的脚步让自己的swift技能熟练
在实际项目中实践,在书写记录中巩固,每日一记。

你可能感兴趣的:(Swift学习记录 Project 6)