Study iOS 9 Programming with Swift(4)

2016/09/01 posted in  Develop comments

使用Swift进行iOS编程知识点总结第四篇。

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

目录

更新记录

  • 2016/09/01 第一次发布

Self Sizing Cells

  • 设置好cell的Constraints后,并且要把Cell的number of lines的值设置为0,在viewDidLoad方法中添加以下代码:
tableView.estimatedRowHeight = 36.0 
tableView.rowHeight = UITableViewAutomaticDimension

Unwind Segue

  • 要点击一个button关闭一个view返回上一级view,可以在destination view controller中添加如下代码,并且将button与exit连接。
  • 代码片段:
// 从review view跳转回detail view,并且带回rating的值改变detail view
@IBAction func close(segue:UIStoryboardSegue) {
    
  if let reviewViewController = segue.sourceViewController as? ReviewViewController {
    if let rating = reviewViewController.rating {
      ratingButton.setImage(UIImage(named: rating), forState: UIControlState.Normal)
    }
  }
    
}

实例:多个button触发一个action方法

  • 对于多个button想要触发一个action方法,可以通过为不同的button定义不同的tag,之后coding时通过switch来区分不同tag,从而执行不同的动作。
  • 代码片段:
    // 对电影进行评价,根据点击button的不同给rating赋值并关闭modal view
@IBAction func ratingSelected(sender: UIButton) {
  
    switch (sender.tag) {
    case 100: rating = "dislike"
    case 200: rating = "good"
    case 300: rating = "great"
    default: break
    }
    
    // 执行特定的segue,即unwind to detailview
    performSegueWithIdentifier("unwindToDetailView", sender: sender)
    
}

未完待续