1月 06

swift2 异常处理

enum Error : ErrorType {
    case DivisionError
}
func divide(a: Int, b: Int) throws -> Int {
    if b == 0 {
        //使用throws来指定方法来抛出一个错误
	throw Error.DivisionError
    }
    return a/b
}

do {
    let result = try 
    //运行相应的代码
    divide(a: Int, b: Int)
} catch let Error. DivisionError {
    print("error")
}

转载收藏地址
http://appventure.me/2015/06/19/swift-try-catch-asynchronous-closures/

12月 02

swift2 tableview 滑动删除

部分主要代码

    @IBOutlet weak var userTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        //注册缓存标示
        refreshControl.addTarget(self, action: "refreshData", forControlEvents: UIControlEvents.ValueChanged)
        refreshControl.attributedTitle = NSAttributedString(string: "松手刷新数据")
        userTableView.addSubview(refreshControl)
        refreshData()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func loadView() {
        super.loadView()
    }
    
    //在本例中,只有一个分区
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }

    // 删除部分开始
    //滑动删除必须实现的方法
    func tableView(tableView: UITableView,commitEditingStyle editingStyle: UITableViewCellEditingStyle,
        forRowAtIndexPath indexPath: NSIndexPath) {
            let index = indexPath.row
            self.dataArray.removeAtIndex(index)
            self.userTableView.deleteRowsAtIndexPaths([indexPath],
                withRowAnimation: UITableViewRowAnimation.Top)
    }
    
    //滑动删除
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCellEditingStyle {
            return UITableViewCellEditingStyle.Delete
    }
    
    //修改删除按钮的文字
    func tableView(tableView: UITableView,
        titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
            return "删除"
    }
    //删除部分结束
11月 21

swift2 下拉刷新

@IBOutlet weak var userTableView: UITableView!
var refreshControl = UIRefreshControl()
var dataArray:[JSON]=[]

override func viewDidLoad() {
super.viewDidLoad()
//self.automaticallyAdjustsScrollViewInsets = false
//绑定代理和数据元
userTableView.delegate=self
userTableView.dataSource=self
//注册缓存标示
refreshControl.addTarget(self, action: “refreshData”, forControlEvents: UIControlEvents.ValueChanged)
refreshControl.attributedTitle = NSAttributedString(string: “松手刷新数据”)
userTableView.addSubview(refreshControl)
refreshData()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func loadView() {
super.loadView()
}

//在本例中,只有一个分区
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}

// 刷新数据
func refreshData() {
netManage.request(.GET,url+”/v1/userstocknew”)
.responseJSON{ get in
let get_data=JSON(get.result.value!)[“stockList”]
self.dataArray=get_data.array!
self.userTableView.reloadData()
self.refreshControl.endRefreshing()
}
}

// 统计数据源行数
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count;
}

// 加载数据部分
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let ut=UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: “cell”)
let stockData=JSON(self.dataArray)
ut.textLabel?.textColor=UIColor.redColor()
ut.textLabel?.text=stockData[indexPath.row][“name”].stringValue
let one_data=stockData[indexPath.row][“price”].stringValue+” “+stockData[indexPath.row][“floper”].stringValue
ut.detailTextLabel?.text=one_data
return ut
}