第十一章 静态单元格

本项目是《beginning iOS8 programming with swift》中的项目学习笔记==》全部笔记目录

------------------------------------------------------------------------------------------------------------------

1.    拖一个table view控制器,修改单元格content为static cells,修改rows为5。
2.    第一格:修改高度为250,拖一个image view,设置image为camera,mode为Aspect Fit。
3.    第二格:修改高度为72,拖一个Label,设为Name,拖一个text field,设置placeholder为Restaurant Name。
剩下的类似,如下图:

5. 包一个导航控制器,设置title为New Restaurant。
6. 在主控制器的导航栏上拖一个bar button item,设置identifier为Add,连线到上面的控制器(Modal,identifier为addRestaurant)。
7. 在New Restaurant的导航栏左边拖一个bar button item,设置文字为Cancel。
8. 在主控制器中增加一个unwind方法,绑定到Cancel按钮:

 

@IBAction func unwindToHomeScreen(segue:UIStoryboardSegue) {
}

9. 新建一个继承自UITableViewController的控制器类AddTableViewController,并关联到IB。
10. 去掉numberOfSectionsInTableView方法和numberOfRowsInSection方法(我们用静态单元格)。
11. 实现点击第一格单元格,选择照片:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == 0 {
        if UIImagePickerController.isSourceTypeAvailable(.PhotoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.allowsEditing = false
            imagePicker.sourceType = .PhotoLibrary // 或者.Camera
            imagePicker.delegate = self
           
            self.presentViewController(imagePicker, animated: true, completion: nil)
        }
    }
   
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

12. 获取选中图片:
增加一个Outlet,并关联IB:@IBOutlet weak var imageView: UIImageView!
控制器遵守两个协议:UIImagePickerControllerDelegate, UINavigationControllerDelegate。实现代理方法:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    imageView.image = image
    imageView.contentMode = .ScaleToFill
    imageView.clipsToBounds = true
   
    dismissViewControllerAnimated(true, completion: nil)
}

13. Bug修改:
选择完照片后,控制器状态栏文字颜色变了。实现下面方法修复:

func navigationController(navigationController: UINavigationController!,
    willShowViewController viewController: UIViewController!, animated: Bool) {
    UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: false)
}

提高:
实现保存按钮的方法(数据验证,保存,关闭控制器),切换YES/NO按钮的颜色
需要用到下面的方法:performSegueWithIdentifier(unwindToHomeScreen, sender: self)    

 

posted @ 2015-03-02 09:59  唐小喵  阅读(841)  评论(0编辑  收藏  举报