Skip to content

Instantly share code, notes, and snippets.

View JeremyXue77's full-sized avatar
🐈
Keep going

Jeremy Xue JeremyXue77

🐈
Keep going
View GitHub Profile
func multipleDelete() {
// 存放刪除的 row
var deleteRows:[Int] = []
// 透過迴圈來遍歷整個 listTableView 的 cell
for row in 0...myTask.count - 1 {
if let cell = listTableView.cellForRow(at: [0,row]) {
// 如果 cell 被選擇了,就加入我們刪除的陣列中
if cell.isSelected {
@IBAction func changeDeleteMode(_ sender: Any) {
// 遍歷 listTableView 將所有被選取的 cell 取消選取
for row in 0...myTask.count - 1{
listTableView.cellForRow(at: [0,row])?.isSelected = false
}
// 判斷左上角的 BarButtonItem.title,進行相對的模式切換
if self.navigationItem.leftBarButtonItem?.title == "Single" {
self.navigationItem.leftBarButtonItem?.title = "Multiple"
@IBAction func addTask(_ sender: Any) {
// Check taskTextField text
if taskTextField.text == "" {
showAlert()
return
}
myTask.append(taskTextField.text!)
listTableView.insertRows(at: [[0,myTask.count - 1]], with: UITableViewRowAnimation.right)
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case UITableViewCellEditingStyle.delete:
myTask.remove(at: indexPath.row)
listTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)
case UITableViewCellEditingStyle.insert:
if let isSelected = tableView.cellForRow(at: indexPath)?.isSelected {
tableView.cellForRow(at: indexPath)?.isSelected = !isSelected
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
listTableView.setEditing(editing, animated: true)
if deleteMode == UITableViewCellEditingStyle.insert {
multipleDelete()
saveData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
myTableView.dataSource = self
myTableView.delegate = self
refreshControl = UIRefreshControl()
myTableView.addSubview(refreshControl)
}
func loadData(){
// 這邊我們用一個延遲讀取的方法,來模擬網路延遲效果(延遲3秒)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
// 停止 refreshControl 動畫
self.refreshControl.endRefreshing()
// 新建5筆假資料
for _ in 1...5 {
self.data.append(self.data.count + 1)
self.myTableView.insertRows(at: [[0,self.data.count - 1]], with: UITableViewRowAnimation.fade)
@IBAction func refreshData(_ sender: Any) {
// 開始刷新動畫
refreshControl.beginRefreshing()
// 使用 UIView.animate 彈性效果,並且更改 TableView 的 ContentOffset 使其位移
// 動畫結束之後使用 loadData()
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.myTableView.contentOffset = CGPoint(x: 0, y: -self.refreshControl.bounds.height)
}) { (finish) in
@IBAction func shareInfo(_ sender: UIButton) {
// activityItems 陣列中放入我們想要使用的元件,這邊我們放入使用者圖片、使用者名稱及個人部落格。
// 這邊因為我們確認裡面有值,所以使用驚嘆號強制解包。
let activityVC = UIActivityViewController(activityItems: [userImage.image!,userName.text!,userBlog.text!], applicationActivities: nil)
// 顯示出我們的 activityVC。
self.present(activityVC, animated: true, completion: nil)
}
@IBAction func shareInfo(_ sender: UIButton) {
let activityVC = UIActivityViewController(activityItems: [userImage.image!,userName.text!,userBlog.text!], applicationActivities: nil)
activityVC.completionWithItemsHandler = {(activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
// 如果錯誤存在,跳出錯誤視窗並顯示給使用者。
if error != nil {
self.showAlert(title: "Error", message: "Error:\(error!.localizedDescription)")
return
}