swift中tableViewCell的操作

在iOS的開發(fā)過程中绵载,tableView的使用永遠(yuǎn)都是最常用的控件。今天學(xué)習(xí)了一下tableViewCell的操作。代碼并不是很復(fù)雜娃豹,如果有OC開發(fā)經(jīng)驗(yàn)的人焚虱,應(yīng)該很容易看懂的。


classViewController:UIViewController,UITableViewDelegate,UITableViewDataSource

{

//    因?yàn)镾wift中要求變量或常量在聲明時(shí)就要初始化其值懂版,所以我們在實(shí)際開發(fā)中鹃栽,聲明變量或常量時(shí)使用可選類型。后面代碼中使用到以上變量時(shí)需要加 ! 對其解包躯畴。

vartableView:UITableView?

//    var items :NSMutableArray?

// 例如可以不用可選民鼓,

varitems:NSMutableArray= []

varleftBtn:UIButton?

overridefuncviewDidLoad() {

super.viewDidLoad()

self.items=NSMutableArray()

// 初始化tableView

setupViews()

// 右邊添加按鈕

setupRightBarButtonItem()

// 左邊刪除按鈕

setupLeftBarButtonItem();

}

funcsetupViews()

{

self.tableView=UITableView(frame:self.view!.frame)

self.tableView!.delegate=self

self.tableView!.dataSource=self

self.tableView!.registerClass(UITableViewCell.self,forCellReuseIdentifier:"cell")

self.view.addSubview(self.tableView!)

}

funcsetupLeftBarButtonItem()

{

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=100

self.leftBtn!.userInteractionEnabled=false

self.leftBtn!.addTarget(self,action:"leftBarButtonItemClicked",forControlEvents:UIControlEvents.TouchUpInside)

letbarButtonItem=UIBarButtonItem(customView:self.leftBtn!)

self.navigationItem.leftBarButtonItem=barButtonItem

}

funcsetupRightBarButtonItem()

{

letbarButtonItem=UIBarButtonItem(title:"Add",style:UIBarButtonItemStyle.Plain,target:self,action:"rightBarButtonItemClicked")

self.navigationItem.rightBarButtonItem=barButtonItem

}

funcrightBarButtonItemClicked()

{

letrow=self.items.count

letindexPath=NSIndexPath(forRow:row,inSection:0)

self.items.addObject("1")

self.tableView?.insertRowsAtIndexPaths([indexPath],withRowAnimation:UITableViewRowAnimation.Left)

self.leftBtn!.userInteractionEnabled=true

}

funcleftBarButtonItemClicked()

{

if(self.leftBtn!.tag==100)

{

// 設(shè)置當(dāng)前tableView可編輯

self.tableView?.setEditing(true,animated:true)

self.leftBtn!.tag=200

self.leftBtn?.setTitle("Done",forState:UIControlState.Normal)

}

else

{

self.tableView?.setEditing(false,animated:true)

self.leftBtn!.tag=100

self.leftBtn?.setTitle("Edit",forState:UIControlState.Normal)

}

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

functableView(tableView:UITableView,numberOfRowsInSectionsection:Int) ->Int

{

returnself.items.count

}

functableView(tableView:UITableView,cellForRowAtIndexPathindexPath:NSIndexPath) ->UITableViewCell

{

letcell=tableView.dequeueReusableCellWithIdentifier("cell",forIndexPath:indexPath)asUITableViewCell

cell.textLabel!.text=String(format:"%i",indexPath.row+1)

returncell

}

// 指定可編輯的行

functableView(tableView:UITableView,setEditingindexPath:NSIndexPath) ->Bool

{

returntrue

}

// 編輯完成

functableView(tableView:UITableView,commitEditingStyleeditingStyle:UITableViewCellEditingStyle,forRowAtIndexPathindexPath:NSIndexPath)

{

self.items.removeObjectAtIndex(indexPath.row)

self.tableView?.deleteRowsAtIndexPaths([indexPath],withRowAnimation:UITableViewRowAnimation.Top)

if(self.items.count==0)

{

self.leftBtn!.userInteractionEnabled=false

}

}

// 編輯樣式

functableView(tableView:UITableView,editingStyleForRowAtIndexPathindexPath:NSIndexPath) ->UITableViewCellEditingStyle

{

return(UITableViewCellEditingStyle.Delete)

}

// 指定哪些行可以移動(dòng)

functableView(tableView:UITableView,canMoveRowAtIndexPathindexPath:NSIndexPath) ->Bool

{

returntrue

}

// 移動(dòng)完成

functableView(tableView:UITableView,moveRowAtIndexPathsourceIndexPath:NSIndexPath,toIndexPathdestinationIndexPath:NSIndexPath)

{

self.tableView?.moveRowAtIndexPath(sourceIndexPath,toIndexPath:destinationIndexPath)

self.items.exchangeObjectAtIndex(sourceIndexPath.row,withObjectAtIndex:destinationIndexPath.row)

}

// 限制在同一分區(qū)移動(dòng)

functableView(tableView:UITableView,targetIndexPathForMoveFromRowAtIndexPathsourceIndexPath:NSIndexPath,toProposedIndexPathproposedDestinationIndexPath:NSIndexPath) ->NSIndexPath{

ifsourceIndexPath.section==proposedDestinationIndexPath.section{

returnproposedDestinationIndexPath

}

returnsourceIndexPath

}

// 點(diǎn)擊cell

functableView(tableView:UITableView,didSelectRowAtIndexPathindexPath:NSIndexPath)

{

print("row = %d",indexPath.row)

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蓬抄,隨后出現(xiàn)的幾起案子丰嘉,更是在濱河造成了極大的恐慌,老刑警劉巖倡鲸,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件供嚎,死亡現(xiàn)場離奇詭異,居然都是意外死亡峭状,警方通過查閱死者的電腦和手機(jī)克滴,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來优床,“玉大人劝赔,你說我怎么就攤上這事〉ǔǎ” “怎么了着帽?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長移层。 經(jīng)常有香客問我仍翰,道長,這世上最難降的妖魔是什么观话? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任予借,我火速辦了婚禮,結(jié)果婚禮上频蛔,老公的妹妹穿的比我還像新娘灵迫。我一直安慰自己,他們只是感情好晦溪,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布瀑粥。 她就那樣靜靜地躺著,像睡著了一般三圆。 火紅的嫁衣襯著肌膚如雪狞换。 梳的紋絲不亂的頭發(fā)上避咆,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機(jī)與錄音哀澈,去河邊找鬼牌借。 笑死度气,一個(gè)胖子當(dāng)著我的面吹牛割按,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播磷籍,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼适荣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了院领?” 一聲冷哼從身側(cè)響起弛矛,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎比然,沒想到半個(gè)月后丈氓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡强法,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年万俗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片饮怯。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡闰歪,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蓖墅,到底是詐尸還是另有隱情库倘,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布论矾,位于F島的核電站教翩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏贪壳。R本人自食惡果不足惜饱亿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望寥袭。 院中可真熱鬧路捧,春花似錦、人聲如沸传黄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽膘掰。三九已至章姓,卻和暖如春佳遣,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背凡伊。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工零渐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人系忙。 一個(gè)月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓诵盼,卻偏偏與公主長得像,于是被迫代替她去往敵國和親银还。 傳聞我的和親對象是個(gè)殘疾皇子风宁,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件蛹疯、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,094評論 4 62
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,080評論 25 707
  • 準(zhǔn)備工作 “工欲善其事必先利其器戒财。” 1.電腦不一定要配置高捺弦,但是雙屏是必須的饮寞,越大越好,能一個(gè)橫屏一個(gè)豎屏更好列吼。...
    霧月之夢閱讀 435評論 0 0
  • 序言 第一部分戀愛與自信的“道” 第一章幽崩、核心自信 第1節(jié)—自信的根源 第2節(jié)—自信與狀態(tài) 第3節(jié)—如何提升核心自...
    迎刃閱讀 5,698評論 1 8
  • 微信的朋友圈之所以火起來,關(guān)鍵能讓遠(yuǎn)在天邊近在眼前的朋友聚集在一個(gè)圈子里自由發(fā)表言論冈欢,曬各種幸福歉铝。這個(gè)圈子那個(gè)圈子...
    114號別墅閱讀 636評論 0 0