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í)就要初始化其值,所以我們?cè)趯?shí)際開發(fā)中,聲明變量或常量時(shí)使用可選類型。后面代碼中使用到以上變量時(shí)需要加 ! 對(duì)其解包。

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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,213評(píng)論 4 61
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,983評(píng)論 25 709
  • 準(zhǔn)備工作 “工欲善其事必先利其器?!?1.電腦不一定要配置高,但是雙屏是必須的,越大越好,能一個(gè)橫屏一個(gè)豎屏更好。...
    霧月之夢(mèng)閱讀 471評(píng)論 0 0
  • 序言 第一部分戀愛與自信的“道” 第一章、核心自信 第1節(jié)—自信的根源 第2節(jié)—自信與狀態(tài) 第3節(jié)—如何提升核心自...
    迎刃閱讀 5,922評(píng)論 1 8
  • 微信的朋友圈之所以火起來,關(guān)鍵能讓遠(yuǎn)在天邊近在眼前的朋友聚集在一個(gè)圈子里自由發(fā)表言論,曬各種幸福。這個(gè)圈子那個(gè)圈子...
    114號(hào)別墅閱讀 759評(píng)論 0 0

友情鏈接更多精彩內(nèi)容