tableViewCell的操作

在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)}}

?

轉載于:https://www.cnblogs.com/wangxianggudu/p/5107733.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396988.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396988.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396988.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

stm32正交編碼器 原理圖_惡劣環境下應用的電感式增量編碼器和絕對編碼器

編碼器可分為兩種基本類型 - 增量編碼器和絕對編碼器。增量編碼器的顯著特征是它報告角度的變化。換句話說,當增量編碼器通電時,它不會報告其角位置,直到它具有測量的參考點。絕對編碼器明確地在比例或范圍內報告其位置。換句話說&#xff0c…

【SqlServer】Sqlserver中的DOS命令操作

輸入osql ?查看是否支持當前版本,如果是SQL Server 2005以上用Sqlcmd , 以下用Osql連接數據庫(a)Osql -S localhost -U username -P password(SQL Server身份驗證,需要用戶民和密碼)(b)Osql -S localhos…

微信小程序內訓筆記

2016年9月22日凌晨微信官方正式宣布“小程序”開始內測,有“微信之父”之稱、騰訊集團高級執行副總裁張小龍在2016年末對外宣布“小程序“應用將于2017年1月9日正式推出 這一次微信還是按照慣例,通過機器跑出的數據,首先將“小程序”開放給了…

python基礎代碼的含義_Python基礎學習篇

原標題:Python基礎學習篇 1、編碼 默認情況下,Python 3 源碼文件以 UTF-8 編碼,所有字符串都是unicode 字符串。 當然你也可以為源碼文件指定不同的編碼:# -*- coding: cp-1252 -*- 2、標識符 第一個字符必須是字母表中字母或下劃…

java面向對象super_【JavaSE】面向對象之super、final

一、super關鍵字它是一個指代變量,用于在子類中指代父類對象。1.作用指代父類對象區分子父類同名的成員變量,區分父類中成員變量和子類中同名的局部變量2.使用與this相同,調用父類成員變量和成員方法:super.xx super.xxx()調用父類…

Week_1_Physical Electronics and Semiconductors

Semiconductors Fundamentals Type of solids 轉載于:https://www.cnblogs.com/ronnielee/p/9579783.html

【Linux高頻命令專題(23)】tar

概述 通過SSH訪問服務器,難免會要用到壓縮,解壓縮,打包,解包等,這時候tar命令就是是必不可少的一個功能強大的工具。linux中最流行的tar是麻雀雖小,五臟俱全,功能強大。 tar命令可以為linux的文…

2. Add Two Numbers

直接用一個carry記錄進位就可以 1 //NEW2 class Solution {3 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {4 ListNode root new ListNode(0);5 return addTwoNumbers(l1, l2, root);6 }7 public ListNode addTwoNumbers(ListNode …

安裝Windows更新程序遇到錯誤:0x80070422

看看服務那里 windows update服務是不是被禁用了? 還有一個問題可能是由于Windows Modules Installer被禁用了。

談談對python的理解_淺談對python pandas中 inplace 參數的理解

這篇文章主要介紹了對python pandas中 inplace 參數的理解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 pandas 中 inplace 參數在很多函數中都會有,它的作用是:是否在原對象基礎上進行修改 inplace True&am…

java中 hashset_Java中的HashSet

HashSet擴展AbstractSet并實現Set接口。它創建一個使用哈希表進行存儲的集合。哈希表通過使用稱為哈希的機制來存儲信息。在散列中,鍵的信息內容用于確定唯一值,稱為其散列碼。然后,將哈希碼用作存儲與鍵關聯的數據的索引。鍵到其哈希碼的轉換…

mac下的svn服務器建立

MAC下的SVN服務器建立: from : http://blog.csdn.net/q199109106q/article/details/8655204 在Windows環境中,我們一般使用TortoiseSVN來搭建svn環境。在Mac環境下,由于Mac自帶了svn的服務器端和客戶端功能,所以我們可以在不裝任何第三方軟件…

SQL手冊

一.SQL簡介 二.SQL數據類型 三.SQL語法 四.SQL SELECT語句 五.SQL INSERT語句 六.SQL UPDATE語句 七.SQL DELETE語句 八.SQL DROP語句 九.SQL CREDTE語句 十.SQL ALTER 語句總結 十一.SQL事務 十二.函數總結 十三.數據庫其他操作 十四.MySQL 、SQL MS Access、和 SQL Server 數…

python語音合成并播放_使用Python實現文字轉語音并生成wav文件的例子

目前手邊的一些工作,需要實現聲音播放功能,而且僅支持wav聲音格式。 現在,一些網站上支持文字轉語音功能,但是生成的都是MP3文件,這樣還需要額外的軟件來轉成wav文件,十分麻煩。 后來,研究Pytho…

php 字符串 替換 最后,php如何替換字符串中的最后一個字符

php替換字符串中的最后一個字符的方法是:可以通過preg_replace()函數來實現。該函數的語法為:【preg_replace(mixed $pattern, mixed $replacement, mixed $subject】。要替換字符串中的最后一個字符,可以通過preg_replace()函數來實現。(如果…

生日轉換成星座

/// <summary>/// 生日轉換成星座/// </summary>/// <param name"birthday"></param>/// <returns></returns>public static string GetAstro(DateTime birthday){string value string.Empty;int month birthday.Month;int da…

logback的使用和logback.xml詳解

原文地址&#xff1a;https://www.cnblogs.com/warking/p/5710303.html#4046335 作者&#xff1a;行走在云端的愚公 一、logback的介紹   Logback是由log4j創始人設計的另一個開源日志組件,官方網站&#xff1a; http://logback.qos.ch。它當前分為下面下個模塊&#xff1a; …

tcp協議的主要功能是什么_前端要知道的網絡知識一:TCP/IP 協議到底在講什么...

你之所以不知道那套書在講什么&#xff0c;是因為你還沒有認識到網絡協議有什么用&#xff0c;怎么用&#xff0c;以什么形式在使用&#xff0c;網絡協議的概念很簡單&#xff0c;就幾句話&#xff0c;你只知道網絡協議的概念&#xff0c;只知道很多大神都推薦這套書&#xff0…

php統計字符個數,php中3種方法統計字符串中每種字符的個數并排序

//這個方法純粹是背函數&#xff0c;不解釋&#xff1b;function countStr($str){$str_arraystr_split($str);$str_arrayarray_count_values($str_array);arsort($str_array);return $str_array;}//以下是例子&#xff1b;$str"asdfgfdas323344##$\$fdsdfg*$**$*$**$$4435…

mysql創建定時器(event),查看定時器,打開定時器,設置定時器時間

為什么80%的碼農都做不了架構師&#xff1f;>>> 由于項目需要創建定時器&#xff08;evevt&#xff09;&#xff0c;所以就百度了一下&#xff0c;發現基本都是來源于一個模板&#xff0c;有些功能還不全&#xff0c;現在自己總結一下。 注&#xff1a;mysql版本是…