//
// ViewController.swift
// try
//
// Created by Stary on 3/28/16.
// Copyright © 2016 Stary. All rights reserved.
//
import UIKit
class NewTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var array = [String](count: 50, repeatedValue: "yan")
var arrayIsMarked = [Bool](count: 51, repeatedValue: false)
// 必备的两个函数。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array.count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
// 对每一个cell的操作。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// 每个自定义的cell都有一个identify
let identistring = "trys"
let cell = tableView.dequeueReusableCellWithIdentifier(identistring, forIndexPath: indexPath) as! NewTableViewCell
cell.ClassLabel.text = "Variable"
cell.DescriptionLabel.text = "Let's try"
cell.NewimageView.image = UIImage(named: "p1.jpg")
if arrayIsMarked[indexPath.row] {
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
return cell
}
// 按住某个cell的反应。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let controller = UIAlertController(title: "Thanks", message: "\(array[0])", preferredStyle: .Alert)
let okAction = UIAlertAction(title: "Ok", style: .Default, handler: {
(action : UIAlertAction) -> Void in
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
self.arrayIsMarked[indexPath.row] = true
})
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let alert = UIAlertAction(title: "Cancel", style: .Cancel, handler: {
(action : UIAlertAction) -> Void in
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = UITableViewCellAccessoryType.None
self.arrayIsMarked[indexPath.row] = false
})
controller.addAction(alert)
controller.addAction(okAction)
self.presentViewController(controller, animated: true, completion: nil)
}
// 把一个cell左拉后会出现的按键。
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Destructive, title: "Delete", handler: {
(action: UITableViewRowAction, NSIndexPath) -> Void in
self.array.removeAtIndex(indexPath.row)
self.arrayIsMarked.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)
})
let ChangeAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Change") { (UITableViewRowAction, NSIndexPath) -> Void in
let Controller = UIAlertController(title: "Do you wanna change?", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let YesAction = UIAlertAction(title: "Yes", style: UIAlertActionStyle.Default, handler: {
action in
let controller = UIAlertController(title: "Thank you for your changing", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Bye", style: UIAlertActionStyle.Cancel, handler: nil)
controller.addAction(cancelAction)
self.presentViewController(controller, animated: true, completion: nil)
})
let NoAction = UIAlertAction(title: "No", style: .Cancel, handler: nil)
Controller.addAction(YesAction)
Controller.addAction(NoAction)
self.presentViewController(Controller, animated: true, completion: nil)
}
return [deleteAction, ChangeAction]
}
// 返回每个cell的高度。
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80
}
}
tableView还要注意设定delegate & datasource的委托。