对于swift一直提不起来兴趣,老是不想学。。。前2周公司部门内需要做内部技术分享。这两周以来断断续续的看着,感觉真的是看起来真是没啥大的作用,除非真正的去用。才能发现一些问题。所以,大胆的去看文档吧,边看边实践,你会有不一样的收获的哦。
下面的这个例子呢,很简单,主要就是Swift下的UITableView的数据的展示以及数据增加删除,再就是通知,闭包,代理这三种传值方式,涉及到的知识点还是很多的,比如什么协议,扩展之类的,希望对初学者还是有点帮助的哈。
//页面A的代码哦,全部的哈,仔细看,应该能够看清楚的哈。
import UIKit
class ViewController: UIViewController{
var dataSourceArray = [[String:String]]()
var lyanlaTableView:UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.configuerNavigarionView()
self.configuerView()
// self.configureData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//设置导航栏部分
func configuerNavigarionView(){
title = "演示Demo"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Done, target:self, action: Selector("next"))
}
//导航栏右按钮的点击方法
func next(){
let inputVc = InputDetailViewController()
// inputVc.delegate = self
inputVc.lyanlaClosure = reloadTableViewData
navigationController?.pushViewController(inputVc, animated: true)
}
//设置表格
func configuerView(){
lyanlaTableView = UITableView(frame: CGRectMake(0, 20,view.frame.size.width, view.frame.size.height), style: UITableViewStyle.Plain)
lyanlaTableView.delegate = self
lyanlaTableView.dataSource = self
view.addSubview(lyanlaTableView)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
lyanlaTableView.reloadData()
}
/**
注册通知
*/
func registerNotification(){
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadDataNotiy:", name:LyanlaNotification, object: nil)
}
//这个是最初的测试数据啦,哈哈哈
func configureData(){
dataSourceArray.append(["title":"Wyy","content":"在很久很久以前,你拥有我,我拥有你"])
dataSourceArray.append(["title":"Mww","content":"每当夕阳西沉的时候"])
dataSourceArray.append(["title":"Qq","content":"天空虽然飘着雨,我依然等待你的归期"])
}
}
//以下是对ViewController写了3个扩展,并各自实现了一些协议的方法。分开写是为了更加的清楚明了哈
extension ViewController:UITableViewDataSource{
/**
tableView datasource method
*/
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return dataSourceArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell(style:UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
cell.textLabel?.text = dataSourceArray[indexPath.row]["title"]
cell.detailTextLabel?.text = dataSourceArray[indexPath.row]["content"]
return cell
}
}
extension ViewController:UITableViewDelegate{
/**
tableView delegate method
*/
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
return 60.0
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
dataSourceArray .removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.Delete
}
}
extension ViewController:LyanlaPassValueDelegate{
//监听通知调用的方法
func reloadDataNotiy(notification: NSNotification){
let dic = notification.object as! [String:String]
print(dic)
dataSourceArray.append(dic)
lyanlaTableView.reloadData()
}
/**
实现代理方法以及闭包调用方法
*/
func reloadTableViewData(string1:String,string2:String){
dataSourceArray.append(["title":string1,"content":string2])
lyanlaTableView.reloadData()
}
}
import UIKit
let LyanlaNotification:String = "ReloadDataNotification"
//委托代理模式
protocol LyanlaPassValueDelegate:NSObjectProtocol{
//回调方法
func reloadTableViewData(string1:String,string2:String)
}
typealias sendValueClosure = (string1:String,string2:String) -> Void
class InputDetailViewController: UIViewController,UITextFieldDelegate{
//声明一个代理
var delegate:LyanlaPassValueDelegate?
//声明一个闭包
var lyanlaClosure:sendValueClosure?
var inputText:String?
override func viewDidLoad() {
super.viewDidLoad()
self.configuerNavigarionView()
self.configuerView()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func configuerNavigarionView(){
navigationItem.title = "输入"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target:self, action: Selector("save"))
}
func save(){
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let dateString = formatter.stringFromDate(date)
if (delegate) != nil{
//代理传值
delegate?.reloadTableViewData(inputText!,string2: dateString)
}
//闭包传值
if (lyanlaClosure != nil) {
print(inputText)
print(dateString)
lyanlaClosure!(string1: inputText!,string2: dateString)
}
//通知传值
NSNotificationCenter.defaultCenter().postNotificationName(LyanlaNotification, object: ["title":inputText!,"content":dateString])
navigationController?.popViewControllerAnimated(true)
}
func configuerView(){
view.backgroundColor = UIColor.whiteColor()
let inputView = UITextField(frame: CGRectMake(30, 100, view.frame.size.width-60, 40))
inputView.layer.borderColor = UIColor.redColor().CGColor
inputView.layer.borderWidth = 1
inputView.layer.cornerRadius = 6.0
inputView.placeholder = "请输入..."
inputView.delegate = self
view.addSubview(inputView)
}
func textFieldShouldReturn(textField: UITextField) -> Bool{
textField.resignFirstResponder()
inputText = textField.text
return true
}
}
运行起来的大致效果就是这样子的哈。
继续加油哈。哈哈哈