第十一章 静态单元格

本项目是《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 @   唐小喵  阅读(845)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示