Study iOS 9 Programming with Swift(2)

2016/08/13 posted in  Develop comments

使用Swift进行iOS编程知识点总结第二篇,主要包括Table View和Table View Controller的区分使用,对Table View的Cells进行自定义,为每个cell添加分享动作,自定义tableview,图片圆角处理、模糊化处理等。

关联阅读:
Study iOS 9 Programming with Swift(1)
Study iOS 9 Programming with Swift(2)
Study iOS 9 Programming with Swift(3)

目录

更新记录

  • 2016/08/15 第一次发布

Table View & Table View Controller

  • 相对于View Controller + Table View, Table View Controller已经提供了DataSource和Delegate,而且针对Table View提前配置好了一些参数,但在某些方面也失去了灵活性。

实例:自定义Table View Cells

  • 对cell的Identifier定义为Cell→在Cell中加入需要的View,如Label、Image View等,并根据设计图添加constraints→因为cell是与UITableViewCell class关联的,所以创建一个新的class,定义为UITableViewCell的subclass
  • Coding:
    • 思路:对cell中自定义的views创建outlet变量→声明要展示的数据变量并赋值→告知table view有几个cell、几个section→在每个cell中显示具体的值。
    • Class MovieTableViewCell代码片段及Class MovieTableViewController代码片段:
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var yearLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var actorLabel: UILabel!
@IBOutlet weak var thumbnailImageView: UIImageView!
	  // 告知tableview中有几个section
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
        return 1
        
    }
    
    // 告知tableview中有几个cell
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        // 根据数组的数量返回table view中cell的数量
        return movieNames.count
        
    }
    
    // 在每个cell中显示具体的内容,这个方法会在table view每个row要展示的时候被调起
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        // 为节省资源,使每个cell可以重复利用
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MovieTableViewCell //downcasting: covert the returned object of dequeueReusableCellWithIdentifier to MovieTableViewCell (对于downcast以及as! & as?的区分与使用还没完全搞懂)
        
        // 定义cell要显示的内容
        cell.nameLabel.text = movieNames[indexPath.row]
        cell.thumbnailImageView.image = UIImage(named: movieImages[indexPath.row])
        cell.yearLabel.text = movieYear[indexPath.row]
        cell.typeLabel.text = movieType[indexPath.row]
        cell.actorLabel.text = movieActor[indexPath.row]
        
        return cell
        
    }

实例:将图片四个角设置为圆角/将正方形图片设置为圆形图片

  • 方法一:通过代码实现
    • 代码片段:
// 将图片设置为圆角,圆角半径设置为3
        cell.thumbnailImageView.layer.cornerRadius = 3.0
        cell.thumbnailImageView.clipsToBounds = true
  • 方法二:通过Xcode配置实现
    • 配置步骤:在storyboard中选择imageview后,在右侧的Identity inspector中的“Use Defined Runtime Attributes”中添加一个值→Key Path定义为“layer.cornerRadius",Type定义为”Number“,Value定义为你要设置的圆角半径值。加入一张60*60的正方形图片,设置半径为30,就可以将其变为圆形→切换到Attributes Inspector,将Clip Subviews打钩。

实例:对table view中的cell添加Swipe动作——分享

  • 思路:定义分享button的title和style→定义点击分享button后的动作,包括定义分享的文本和图片信息→展示ActivityView。其中share button的颜色可以通过backgroudColor属性来进行自定义。
  • 代码片段:
// 对每一个Row添加Swipe Action,如分享、删除等
override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    
    // Social Sharing Button
    let shareAction = UITableViewRowAction(style: .Default, title: "分享", handler: {(action, indexPath) -> Void in
        
        // 定义Share的文本和图片信息,并显示ActivityView
        let defaultText = "Just checking in at" + self.movieNames[indexPath.row]
        if let imageToShare = UIImage(named: self.movieImages[indexPath.row]){
            let activityController = UIActivityViewController(activityItems: [defaultText, imageToShare], applicationActivities: nil)
            self.presentViewController(activityController, animated: true, completion: nil)
        }
    })
    
    // 自定义share action的背景色
    shareAction.backgroundColor = UIColor(red: 25.0/255.0, green: 118.0/255.0, blue: 210.0/255.0, alpha: 1.0)
    
    return [shareAction]
    
}

实例:取消row点击后被选中的状态

  • 代码片段:
// 定义Row被选择之后的动作
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
    // Row点击后取消被选中状态
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
        
}

实例:对table view的分隔线、背景色等进行自定义

  • 代码片段:
//View被加载到内存后,以下方法被调起,且只被调起一次,加载好就不再调起
override func viewDidLoad() {
    super.viewDidLoad()
                
    // 自定义tableview的背景色,注意每个cell也有自己的背景色,cell没有内容时才能看到tableview的背景色
	tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.2)
    
    // 通过设置FooterView,移除空的row的分隔线
    tableView.tableFooterView = UIView(frame: CGRectZero)
    
    // 自定义分隔线的颜色
    tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.8)
    
}
  • 若想调整cell的背景色或者设置为透明,可在tableView(_:cellForRowAtIndexPath:)方法中添加以下代码片段:
// 清除cell的背景色,也即变成透明
cell.backgroundColor = UIColor.clearColor()

实例:对图片采用系统自带的模糊化处理

  • 在viewDidLoad()中添加以下代码片段:
// 对背景图进行模糊化处理,style有三种:Dark,ExtraLight,Light
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
movieBackgroudImageView.addSubview(blurEffectView)