Models: UserModel.swift

Views: UserInfoCell.swift

Controllers: RootViewController.swift, DetailViewController.swift

AppDelegate.swift:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary? ) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
//
let rootController = RootViewController(style: UITableViewStyle.Plain)
let rootNav = UINavigationController(rootViewController: rootController)
self.window!.rootViewController = rootNav
//
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
return true
}
}

UserModel.swift

import Foundation

//
// @brief The model of user, using to store user datas
// @author huangyibiao
//
class UserModel : NSObject {
var userName: String ///< store user's name, optional
var userID: Int ///< store user's ID
var phone: String? ///< store user's telephone number
var email: String? ///< store user's email // designated initializer
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:

import Foundation
import UIKit //
// @brief The cell of showing user infos
// @author huangyibiao
//
class UserInfoCell : UITableViewCell {
var userNameLabel : UILabel!
var phoneLabel : UILabel!
var emailLabel : UILabel! init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier) userNameLabel = UILabel(frame: CGRectMake(30, 0, 100, 44))
userNameLabel.backgroundColor = UIColor.clearColor()
userNameLabel.font = UIFont.systemFontOfSize(14)
self.contentView.addSubview(userNameLabel) phoneLabel = UILabel(frame: CGRectMake(120, 0, 200, 20))
phoneLabel.backgroundColor = UIColor.clearColor()
phoneLabel.font = UIFont.systemFontOfSize(12)
self.contentView.addSubview(phoneLabel) emailLabel = UILabel(frame: CGRectMake(120, 20, 200, 20))
emailLabel.backgroundColor = UIColor.clearColor()
emailLabel.font = UIFont.systemFontOfSize(12)
self.contentView.addSubview(emailLabel)
} func configureCell(userModel: UserModel?) {
if let model = userModel {
userNameLabel.text = model.userName
phoneLabel.text = model.phone
emailLabel.text = model.email
}
}
}

RootViewController.swift:

import Foundation
import UIKit //
// @brief 作为窗体的rootViewControllor
// @author huangyibiao
// class RootViewController : UITableViewController, DetailViewControllerDelegate {
var dataSource = NSMutableArray()
var currentIndexPath: NSIndexPath? override func viewDidLoad() {
super.viewDidLoad() for index in 0...12 {
let model = UserModel(userName: "name:\(index + 1)",
userID: index, phone: "13877747982", email: "632840804@qq.com")
dataSource.addObject(model)
} self.title = "UITableViewDemo"
} override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
} override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
// can't use static? let cellIdentifier: String = "UserInfoCellIdentifier"
// may be no value, so use optional
var cell: UserInfoCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UserInfoCell if cell == nil { // no value
cell = UserInfoCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
} let model: UserModel? = dataSource[indexPath.row] as? UserModel
cell!.configureCell(model) return cell
} override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let detail = DetailViewController()
detail.userModel = dataSource[indexPath.row] as? UserModel
detail.delegate = self
currentIndexPath = indexPath
self.navigationController.pushViewController(detail, animated: true)
} func changeItem(forUserModel userModel: UserModel?) {
var index = 0
for index = 0; index < dataSource.count; index++ {
let model = dataSource[index] as UserModel
if model.userID == userModel?.userID {
model.phone = userModel? .phone
model.email = userModel?.email
tableView.reloadRowsAtIndexPaths([currentIndexPath!], withRowAnimation: UITableViewRowAnimation.Fade)
break
}
}
}
}

DetailViewController.swift:

import Foundation
import UIKit // this delegate needs a @objc, because @optional is only for objective-c, not for swift
@objc protocol DetailViewControllerDelegate : NSObjectProtocol {
@optional func changeItem(forUserModel userModel: UserModel?)
} class DetailViewController : UIViewController {
var userModel: UserModel?
var delegate: DetailViewControllerDelegate? override func viewDidLoad() {
super.viewDidLoad() self.view.backgroundColor = UIColor.whiteColor()
self.title = userModel? .userName let button = UIButton(frame: CGRectMake(10, 200, 300, 40))
button.setTitle("change", forState:UIControlState.Normal)
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: "onChangeButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
} func onChangeButtonClick(sender: UIButton!) {
if userModel {
userModel!.userName = "ChangeName" // changeItem needs to add a ? to the end, before (), because
// this function is optional
// delegate? 表示可能没有代理。而changeItem? 表示方法可能没有实现,这样写就算没有实现也没有问题
delegate?.changeItem? (forUserModel: userModel)
self.navigationController.popViewControllerAnimated(true)
}
}
}

效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29haWZlbjMzNDQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Swift UI学习UITableView and protocol use的更多相关文章

  1. 小波说雨燕 第三季 构建 swift UI 之 UI组件集-视图集(六)Picker View视图 学习笔记

    想对PickerView进行操作,只能在代码中操作. 下面 ,再添加三个label组件,然后将所有组件配置到代码中(看代码),然后要实现对PickerView的操作,就要实现它的DataSource协 ...

  2. Swift UI

    概述 Apple近日发布了Swift编程语言,Swift是供iOS和OS X应用编程的新编程语言.相信很多开发者都在学习这门新语言.   废话不多说,下面我就来学习使用Swift创建一个简单的UI应用 ...

  3. Swift UI开发初探

    今天凌晨Apple刚刚发布了Swift编程语言,Swift是供iOS和OS X应用编程的新编程语言.相信很多开发者都在学习这门新语言. 废话不多说,下面我就来学习使用Swift创建一个简单的UI应用程 ...

  4. Swift开发学习(一):初始篇

    http://blog.csdn.net/powerlly/article/details/29351103 Swift开发学习:初始篇 关于 苹果公司于WWDC2014(Apple Worldwid ...

  5. 一份关于Swift语言学习资源的整理文件

    一份关于Swift语言学习资源的整理文件     周银辉 在这里下载 https://github.com/ipader/SwiftGuide

  6. Swift入门学习之一常量,变量和声明

    版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:http://www.cnblogs.com/cavalier-/p/6059421.html Swift入门学习之一常量,变量和 ...

  7. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  8. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

  9. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

随机推荐

  1. hdu4705(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 题意: 有一颗树, 选出3个点. 不在同一条路径上的集合数. 分析:这题主要能逆向思考下,用总的 ...

  2. POJ 1753 位运算+枚举

    题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...

  3. Linux下玩转Dota2

    Dota2是一款颇为风靡的即时战略类游戏,去年官方就支持Mac和Linux了,对于习惯Mac和linux平台的孩子们来说,简直感动的泪流满面. 当然,也简直是linux程序猿的福音啊啊! 与Win8. ...

  4. excel删除问号~?~

    1.直接替换(菜单)编辑——替换——查找内容——(输入)~?~——替换为(空,就是什么都不输入)——全部替换.2.设原数据在A列,从A1开始,若得到的数值数据需要参与计算,则在B1输入=--LEFT( ...

  5. android maven eclipse里面新建mavenprojectThe desired archetype does not exist

    这个问题头疼死我了 又一次配置下你看我的教程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmFpbmkxMTk=/font/5a6L5L2T/f ...

  6. c++ 按行读取txt文本

    CStdioFile 类的声明保存在 afx.h 头文件中. CStdioFile 类继承自 CFile 类, CStdioFile 对象表示一个用运行时的函数 fopen 打开的 c 运行时的流式文 ...

  7. Thinkpad X200 屏幕备案

    妈妈蛋,屏幕废物前几天(闪屏->暗->变暗),因此,它只能监视房外 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjk2NTg5MA= ...

  8. 百度经纬度和google经纬度互转

    原文:百度经纬度和google经纬度互转 百度地图的坐标转换,由于百度地图在GCJ02协议的基础上又做了一次处理,变为 BD09协议的坐标,以下是坐标的转化方式,可以方便和其他平台转化 private ...

  9. html中返回上一页

    <a href="<a href="javascript :history.back(-1)">返回上一页</a>或<a href=& ...

  10. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...