Swift UI学习之UITableView and protocol use

前端之家收集整理的这篇文章主要介绍了Swift UI学习之UITableView and protocol use前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

Models: UserModel.swift

Views: UserInfoCell.swift

Controllers: RootViewController.swift,DetailViewController.swift


AppDelegate.swift:

  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


UserInfoCell.swift:


RootViewController.swift:

copy @H_404_120@
//@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:


    效果图:

    原文链接:https://www.f2er.com/swift/324606.html

    猜你在找的Swift相关文章