在iOS的開發過程中,tableView的使用永遠都是最常用的控件。今天學習了一下tableViewCell的操作。代碼并不是很復雜,如果有OC開發經驗的人,應該很容易看懂的。
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource { // 因為Swift中要求變量或常量在聲明時就要初始化其值,所以我們在實際開發中,聲明變量或常量時使用可選類型。后面代碼中使用到以上變量時需要加 ! 對其解包。var tableView : UITableView? // var items :NSMutableArray?// 例如可以不用可選,var items :NSMutableArray = []var leftBtn:UIButton?override func viewDidLoad() {super.viewDidLoad()self.items = NSMutableArray()// 初始化tableViewsetupViews()// 右邊添加按鈕setupRightBarButtonItem()// 左邊刪除按鈕setupLeftBarButtonItem();}func setupViews(){self.tableView = UITableView(frame:self.view!.frame)self.tableView!.delegate = selfself.tableView!.dataSource = selfself.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")self.view.addSubview(self.tableView!)}func setupLeftBarButtonItem(){self.leftBtn = UIButton(type: UIButtonType.Custom)self.leftBtn!.frame = CGRectMake(0,0,50,40)self.leftBtn!.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)self.leftBtn!.setTitle("Edit", forState: UIControlState.Normal)self.leftBtn!.tag = 100self.leftBtn!.userInteractionEnabled = falseself.leftBtn!.addTarget(self, action: "leftBarButtonItemClicked", forControlEvents: UIControlEvents.TouchUpInside)let barButtonItem = UIBarButtonItem(customView: self.leftBtn!)self.navigationItem.leftBarButtonItem = barButtonItem}func setupRightBarButtonItem(){let barButtonItem = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBarButtonItemClicked")self.navigationItem.rightBarButtonItem = barButtonItem}func rightBarButtonItemClicked(){let row = self.items.countlet indexPath = NSIndexPath(forRow:row,inSection:0)self.items.addObject("1")self.tableView?.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Left)self.leftBtn!.userInteractionEnabled = true}func leftBarButtonItemClicked(){if (self.leftBtn!.tag == 100){// 設置當前tableView可編輯self.tableView?.setEditing(true, animated: true)self.leftBtn!.tag = 200self.leftBtn?.setTitle("Done", forState: UIControlState.Normal)}else{self.tableView?.setEditing(false, animated: true)self.leftBtn!.tag = 100self.leftBtn?.setTitle("Edit", forState: UIControlState.Normal)}}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{return self.items.count}func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{let cell = tableView .dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCellcell.textLabel!.text = String(format: "%i", indexPath.row+1)return cell}// 指定可編輯的行func tableView(tableView: UITableView, setEditing indexPath: NSIndexPath) -> Bool{return true}// 編輯完成func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){self.items.removeObjectAtIndex(indexPath.row)self.tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)if (self.items.count == 0){self.leftBtn!.userInteractionEnabled = false}}// 編輯樣式func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle{return (UITableViewCellEditingStyle.Delete)}// 指定哪些行可以移動func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool{return true}// 移動完成func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath){self.tableView?.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)self.items.exchangeObjectAtIndex(sourceIndexPath.row, withObjectAtIndex: destinationIndexPath.row)}// 限制在同一分區移動func tableView(tableView: UITableView, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath) -> NSIndexPath {if sourceIndexPath.section == proposedDestinationIndexPath.section {return proposedDestinationIndexPath}return sourceIndexPath}// 點擊cellfunc tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){print("row = %d",indexPath.row)}}
?