加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift UI学习之UITableView and protocol use

发布时间:2020-12-14 07:16:42 所属栏目:百科 来源:网络整理
导读:http://blog.csdn.net/woaifen3344/article/details/29883183 Models: UserModel.swift Views: UserInfoCell.swift Controllers: RootViewController.swift,DetailViewController.swift AppDelegate.swift: [plain] view plain copy print ? importUIKit @UI

http://blog.csdn.net/woaifen3344/article/details/29883183

Models: UserModel.swift

Views: UserInfoCell.swift

Controllers: RootViewController.swift,DetailViewController.swift


AppDelegate.swift:

[plain] view plain copy
print ?
  1. importUIKit
  2. @UIApplicationMain
  3. classAppDelegate:UIResponder,UIApplicationDelegate{
  4. varwindow:UIWindow?
  5. funcapplication(application:UIApplication,didFinishLaunchingWithOptionslaunchOptions:NSDictionary?)->Bool{
  6. self.window=UIWindow(frame:UIScreen.mainScreen().bounds)
  7. //
  8. letrootController=RootViewController(style:UITableViewStyle.Plain)
  9. letrootNav=UINavigationController(rootViewController:rootController)
  10. self.window!.rootViewController=rootNav
  11. self.window!.backgroundColor=UIColor.whiteColor()
  12. self.window!.makeKeyAndVisible()
  13. returntrue
  14. }
  15. }

UserModel.swift

copy
importFoundation
  • //@briefThemodelofuser,usingtostoreuserdatas
  • //@authorhuangyibiao
  • //
  • classUserModel:NSObject{
  • varuserName:String///<storeuser'sname,optional
  • varuserID:Int///<storeuser'sID
  • varphone:String?///<storeuser'stelephonenumber
  • varemail:String?///<storeuser'semail
  • //designatedinitializer
  • init(userName:String,userID:Int,phone:String?,email:String?){
  • self.userName=userName
  • self.userID=userID
  • self.phone=phone
  • self.email=email
  • super.init()
  • }

  • UserInfoCell.swift:

    copy
    importUIKit
  • //@briefThecellofshowinguserinfos
  • //@authorhuangyibiao
  • classUserInfoCell:UITableViewCell{
  • varuserNameLabel:UILabel!
  • varphoneLabel:UILabel!
  • varemailLabel:UILabel!
  • init(style:UITableViewCellStyle,reuseIdentifier:String!){
  • super.init(style:style,reuseIdentifier:reuseIdentifier)
  • userNameLabel=UILabel(frame:CGRectMake(30,100,44))
  • userNameLabel.backgroundColor=UIColor.clearColor()
  • userNameLabel.font=UIFont.systemFontOfSize(14)
  • self.contentView.addSubview(userNameLabel)
  • phoneLabel=UILabel(frame:CGRectMake(120,200,20))
  • phoneLabel.backgroundColor=UIColor.clearColor()
  • phoneLabel.font=UIFont.systemFontOfSize(12)
  • self.contentView.addSubview(phoneLabel)
  • emailLabel=UILabel(frame:CGRectMake(120,20,20))
  • emailLabel.backgroundColor=UIColor.clearColor()
  • emailLabel.font=UIFont.systemFontOfSize(12)
  • self.contentView.addSubview(emailLabel)
  • }
  • funcconfigureCell(userModel:UserModel?){
  • ifletmodel=userModel{
  • userNameLabel.text=model.userName
  • phoneLabel.text=model.phone
  • emailLabel.text=model.email
  • }

  • RootViewController.swift:

    copy
    //@brief作为窗口的rootViewControllor
  • classRootViewController:UITableViewController,DetailViewControllerDelegate{
  • vardataSource=NSMutableArray()
  • varcurrentIndexPath:NSIndexPath?
  • overridefuncviewDidLoad(){
  • super.viewDidLoad()
  • forindexin0...12{
  • letmodel=UserModel(userName:"name:(index+1)",
  • userID:index,phone:"13877747982",email:"632840804@qq.com")
  • dataSource.addObject(model)
  • self.title="UITableViewDemo"
  • overridefunctableView(tableView:UITableView!,numberOfRowsInSectionsection:Int)->Int{
  • returndataSource.count
  • //can'tusestatic?
  • letcellIdentifier:String="UserInfoCellIdentifier"
  • //maybenovalue,souseoptional
  • varcell:UserInfoCell?=tableView.dequeueReusableCellWithIdentifier(cellIdentifier)as?UserInfoCell
  • ifcell==nil{//novalue
  • cell=UserInfoCell(style:UITableViewCellStyle.Default,reuseIdentifier:cellIdentifier)
  • letmodel:UserModel?=dataSource[indexPath.row]as?UserModel
  • cell!.configureCell(model)
  • returncell
  • letdetail=DetailViewController()
  • detail.userModel=dataSource[indexPath.row]as?UserModel
  • detail.delegate=self
  • currentIndexPath=indexPath
  • self.navigationController.pushViewController(detail,animated:true)
  • funcchangeItem(forUserModeluserModel:UserModel?){
  • varindex=0
  • forindex=0;index<dataSource.count;index++{
  • letmodel=dataSource[index]asUserModel
  • ifmodel.userID==userModel?.userID{
  • model.phone=userModel?.phone
  • model.email=userModel?.email
  • tableView.reloadRowsAtIndexPaths([currentIndexPath!],withRowAnimation:UITableViewRowAnimation.Fade)
  • break
  • }


  • DetailViewController.swift:

    copy
    //thisdelegateneedsa@objc,because@optionalisonlyforobjective-c,notforswift
  • @objcprotocolDetailViewControllerDelegate:NSObjectProtocol{
  • @optionalfuncchangeItem(forUserModeluserModel:UserModel?)
  • classDetailViewController:UIViewController{
  • varuserModel:UserModel?
  • vardelegate:DetailViewControllerDelegate?
  • self.view.backgroundColor=UIColor.whiteColor()
  • self.title=userModel?.userName
  • letbutton=UIButton(frame:CGRectMake(10,300,40))
  • button.setTitle("change",forState:UIControlState.Normal)
  • button.backgroundColor=UIColor.redColor()
  • button.addTarget(self,action:"onChangeButtonClick:",forControlEvents:UIControlEvents.TouchUpInside)
  • self.view.addSubview(button)
  • funconChangeButtonClick(sender:UIButton!){
  • ifuserModel{
  • userModel!.userName="ChangeName"
  • //changeItemneedstoadda?totheend,before(),because
  • //thisfunctionisoptional
  • //delegate?表示可能没有代理,而changeItem?表示方法可能没有实现,这样写就算没有实现也没有问题
  • delegate?.changeItem?(forUserModel:userModel)
  • self.navigationController.popViewControllerAnimated(true)
  • }

  • 效果图:

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读