Swift UI学习之UITableView and protocol use

Models: UserModel.Swift

Views: UserInfoCell.swift

Controllers: RootViewController.swift, DetailViewController.swift


AppDelegate.swift:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import UIKit  
  2.   
  3. @UIApplicationMain  
  4. class AppDelegate: UIResponder, UIApplicationDelegate {  
  5.     var window: UIWindow?  
  6.   
  7.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {  
  8.         self.window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  9.         //  
  10.         let rootController = RootViewController(style: UITableViewStyle.Plain)  
  11.         let rootNav = UINavigationController(rootViewController: rootController)  
  12.         self.window!.rootViewController = rootNav  
  13.         //  
  14.         self.window!.backgroundColor = UIColor.whiteColor()  
  15.         self.window!.makeKeyAndVisible()  
  16.         return true  
  17.     }  
  18. }  

UserModel.swift

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import Foundation  
  2.   
  3. //  
  4. // @brief The model of user, using to store user datas  
  5. // @author huangyibiao  
  6. //  
  7. class UserModel : NSObject {  
  8.     var userName: String     ///< store user's name, optional  
  9.     var userID: Int          ///< store user's ID  
  10.     var phone: String?       ///< store user's telephone number  
  11.     var email: String?       ///< store user's email  
  12.       
  13.     // designated initializer  
  14.     init(userName: String, userID: Int, phone: String?, email: String?) {  
  15.         self.userName = userName  
  16.         self.userID = userID  
  17.         self.phone = phone  
  18.         self.email = email  
  19.           
  20.         super.init()  
  21.     }  
  22. }  

UserInfoCell.swift:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import Foundation  
  2. import UIKit  
  3.   
  4. //  
  5. // @brief The cell of showing user infos  
  6. // @author huangyibiao  
  7. //  
  8. class UserInfoCell : UITableViewCell {  
  9.     var userNameLabel : UILabel!  
  10.     var phoneLabel : UILabel!  
  11.     var emailLabel : UILabel!  
  12.       
  13.     init(style: UITableViewCellStyle, reuseIdentifier: String!) {  
  14.         super.init(style: style, reuseIdentifier: reuseIdentifier)  
  15.   
  16.         userNameLabel = UILabel(frame: CGRectMake(30, 0, 100, 44))  
  17.         userNameLabel.backgroundColor = UIColor.clearColor()  
  18.         userNameLabel.font = UIFont.systemFontOfSize(14)  
  19.         self.contentView.addSubview(userNameLabel)  
  20.           
  21.         phoneLabel = UILabel(frame: CGRectMake(120, 0, 200, 20))  
  22.         phoneLabel.backgroundColor = UIColor.clearColor()  
  23.         phoneLabel.font = UIFont.systemFontOfSize(12)  
  24.         self.contentView.addSubview(phoneLabel)  
  25.   
  26.         emailLabel = UILabel(frame: CGRectMake(120, 20, 200, 20))  
  27.         emailLabel.backgroundColor = UIColor.clearColor()  
  28.         emailLabel.font = UIFont.systemFontOfSize(12)  
  29.         self.contentView.addSubview(emailLabel)  
  30.     }  
  31.   
  32.     func configureCell(userModel: UserModel?) {  
  33.         if let model = userModel {  
  34.             userNameLabel.text = model.userName  
  35.             phoneLabel.text = model.phone  
  36.             emailLabel.text = model.email  
  37.         }  
  38.     }  
  39. }  


RootViewController.swift:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import Foundation  
  2. import UIKit  
  3.   
  4. //  
  5. // @brief 作为窗口的rootViewControllor  
  6. // @author huangyibiao  
  7. //  
  8.   
  9. class RootViewController : UITableViewController, DetailViewControllerDelegate {  
  10.     var dataSource = NSMutableArray()  
  11.     var currentIndexPath: NSIndexPath?  
  12.       
  13.     override func viewDidLoad() {  
  14.         super.viewDidLoad()  
  15.           
  16.         for index in 0...12 {  
  17.             let model = UserModel(userName: "name:\(index + 1)",  
  18.                 userID: index, phone: "13877747982", email: "632840804@qq.com")  
  19.             dataSource.addObject(model)  
  20.         }  
  21.           
  22.         self.title = "UITableViewDemo"  
  23.     }  
  24.       
  25.     override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int  {  
  26.         return dataSource.count  
  27.     }  
  28.       
  29.     override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {  
  30.         // can't use static?  
  31.         let cellIdentifier: String = "UserInfoCellIdentifier"  
  32.         // may be no value, so use optional  
  33.         var cell: UserInfoCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UserInfoCell  
  34.           
  35.         if cell == nil { // no value  
  36.             cell = UserInfoCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)  
  37.         }  
  38.           
  39.         let model: UserModel? = dataSource[indexPath.row] as? UserModel  
  40.         cell!.configureCell(model)  
  41.   
  42.         return cell  
  43.     }  
  44.       
  45.     override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {  
  46.        let detail = DetailViewController()  
  47.         detail.userModel = dataSource[indexPath.row] as? UserModel  
  48.         detail.delegate = self  
  49.         currentIndexPath = indexPath  
  50.         self.navigationController.pushViewController(detail, animated: true)  
  51.     }  
  52.       
  53.     func changeItem(forUserModel userModel: UserModel?) {  
  54.         var index = 0  
  55.         for index = 0; index < dataSource.count; index++ {  
  56.             let model = dataSource[index] as UserModel  
  57.             if model.userID == userModel?.userID {  
  58.                 model.phone = userModel?.phone  
  59.                 model.email = userModel?.email  
  60.                 tableView.reloadRowsAtIndexPaths([currentIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade)  
  61.                 break  
  62.             }  
  63.         }  
  64.     }  
  65. }  


DetailViewController.swift:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import Foundation  
  2. import UIKit  
  3.   
  4. // this delegate needs a @objc, because @optional is only for objective-c, not for swift  
  5. @objc protocol DetailViewControllerDelegate : NSObjectProtocol {  
  6.     @optional func changeItem(forUserModel userModel: UserModel?)  
  7. }  
  8.   
  9. class DetailViewController : UIViewController {  
  10.     var userModel: UserModel?  
  11.     var delegate: DetailViewControllerDelegate?  
  12.       
  13.     override func viewDidLoad() {  
  14.         super.viewDidLoad()  
  15.           
  16.         self.view.backgroundColor = UIColor.whiteColor()  
  17.         self.title = userModel?.userName  
  18.           
  19.         let button = UIButton(frame: CGRectMake(10, 200, 300, 40))  
  20.         button.setTitle("change", forState:UIControlState.Normal)  
  21.         button.backgroundColor = UIColor.redColor()  
  22.         button.addTarget(self, action: "onChangeButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)  
  23.         self.view.addSubview(button)  
  24.     }  
  25.       
  26.     func onChangeButtonClick(sender: UIButton!) {  
  27.         if userModel {  
  28.             userModel!.userName = "ChangeName"  
  29.               
  30.             // changeItem needs to add a ? to the end, before (), because  
  31.             // this function is optional  
  32.             // delegate? 表示可能没有代理,而changeItem?表示方法可能没有实现,这样写就算没有实现也没有问题  
  33.             delegate?.changeItem?(forUserModel: userModel)  
  34.             self.navigationController.popViewControllerAnimated(true)  
  35.         }  
  36.     }  
  37. }  

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值