Swift UI学习之UITableView and protocol use

Swift UI学习之UITableView and protocol use,第1张

概述http://blog.csdn.net/woaifen3344/article/details/29883183 Models: UserModel.swift Views: UserInfoCell.swift Controllers: RootViewController.swift, DetailViewController.swift AppDelegate.swift: [plain]

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 @UIApplicationMain classAppDelegate:UIResponder,UIApplicationDelegate{ varwindow:UIWindow? funcapplication(application:UIApplication,dIDFinishLaunchingWithOptionslaunchOptions:NSDictionary?)->Bool{ self.window=UIWindow(frame:UIScreen.mainScreen().bounds) // letrootController=RootVIEwController(style:UItableVIEwStyle.Plain) letrootNav=UINavigationController(rootVIEwController:rootController) self.window!.rootVIEwController=rootNav self.window!.backgroundcolor=UIcolor.whitecolor() self.window!.makeKeyAndVisible() returntrue } }
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:"[email protected]") 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) }
效果图:

总结

以上是内存溢出为你收集整理的Swift UI学习之UITableView and protocol use全部内容,希望文章能够帮你解决Swift UI学习之UITableView and protocol use所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1079776.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存