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