UITableView Basic operations (drop-down refresh, add Delete, group, search, etc.)
注:本小结总结UITableview的一些基本用法
Uitbleview inherits from Uiscrollview, and can only be used to display a column of data (currently only known here), sliding vertically.
Generally there are two ways to achieve, directly with Uitableviewcontroller, occupy the entire screen. Not manually implemented UITableViewDataSource and UITableViewDelegate . The other way is in Uiviewcontroller. Let's see how this works.
let table = UITableView()table.frame = self.view.frameself.view = tabletable.delegate = selftable.dataSource = self
A new one is created here UITableView , and its frame is set to the frame size of the current view. That's full screen. Of course, here you can also set the size you need, the following two sentences is to set its proxy and data source protocol to the current object of course our Viewcontroller is to implement the two Protocols. class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate
Then, add the TableView to the current view or directly assign the current view to it.
Here are a few of the main proxy methods
//返回多少个section func numberOfSectionsInTableView(tableView: UITableView) -> Int { return3 }
Returns the number of sections, section equivalent to grouping, where table is divided into groups. We have three sets of demos here.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case0 : return arr.count case1: return arr1.count case2: return arr2.count default: return0 } }
Then this method is the number of elements in each group. We've used three arrays here.
func initData(){ arr.addObject("ssssddd") arr.addObject("唱什么") arr.addObject("what ") arr.addObject("ssshenme dd") arr1.addObject("我是二部的") arr2.addObject("我是第三个部门的") arr2.addObject("我是第三个部门的1") }
We have initialized these three arrays here. In the viewDidLoad downward use.
Func TableView (Tableview:uitableview, Cellforrowatindexpath indexpath:nsindexpath), UITableViewCell {Let St Rcell ="Cell"var cell = TableView. Dequeuereusablecellwithidentifier(Strcell) if cell = = nil{cell = UITableViewCell (style:uitableviewcellstyle. Subtitle, Reuseidentifier:strcell)} switch Indexpath. section{case0: cell!. Textlabel?. Text= Arr[indexpath. Row] as? String case1: cell!. Textlabel?. Text= Arr1[indexpath. Row] as? String case2: cell!. Textlabel?. Text= Arr2[indexpath. Row] as? String default:cell!. Textlabel?. Text=""}//cell!. Textlabel?. Text= Arr[indexpath. Row] as? String cell!. BackgroundColor=uicolor. Purplecolor() cell?. Contentview. BackgroundColor= Uicolor. Graycolor() return cell! }
This is also the most important way to assign a value to the cell. This is the first to get from tableView.dequeueReusableCellWithIdentifier this, not when the new, because the downloaded data is cached back. We need to find it from the cache pool first. Can not find to take the new, this will be more fluent.
var
Here we declare a refresh control
self.refreshControl = UIRefreshControl()self.refreshControl?.addTarget"onPullToFresh", forControlEvents: UIControlEvents.ValueChanged)self.refreshControl?.attributedTitle"松手就可以刷新啦")self.table.addSubview(refreshControl!)
Here you set the properties of the Shuxin control and give it a registration methodonPullToFresh
Func Onpulltofresh () {//drop-down Refresh Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0)) {Self. Arr. AddObject("SSSSDDD") Self. Arr1. AddObject("What to sing") Self. Arr2. AddObject("What") Self. Arr1. AddObject("Ssshenme DD") Dispatch_async (Dispatch_get_main_queue (), {self. Table. Reloaddata() Self. Refreshcontrol?. endrefreshing() }) } }
This drop-down refresh, first to add data asynchronously, we are here to manually add, but the actual application often need to download data from the network, so relatively slow, asynchronous will be more appropriate. After the download is complete, go back to the main thread to reload the data, and finally stop the drop-down refresh control.
tabletrue
Here we set this property, TableView can be edited, added delete and so on. (Do not add this attribute can only be deleted by left stroke)
Func TableView (Tableview:uitableview, Commiteditingstyle Editingstyle:uitableviewcelleditingstyle, Forrowatindexpath Indexpath:nsindexpath) {if (Editingstyle = = Uitableviewcelleditingstyle.Delete){ //DeleteSwitch Indexpath. Section{ Case 0: Arr.removeobjectatindex (Indexpath.Row) Case 1: Arr1.removeobjectatindex (Indexpath.Row) Case 2: Arr2.removeobjectatindex (Indexpath.Row)default: Print ("No")} tableview.deleterowsatindexpaths ([Indexpath], WithRowAnimation:UITableViewRowAnimation.Fade) }Else if(Editingstyle = = Uitableviewcelleditingstyle.Insert) {switch Indexpath. Section{ Case 0: Arr.insertobject ("What's new??", Atindex:indexpath.Row+1) Case 1: Arr1.insertobject ("What's new??", Atindex:indexpath.Row+1) Case 2: Arr2.insertobject ("What's new??", Atindex:indexpath.Row+1)default: Print ("No")} Let Zyindexpath = Nsindexpath (Forrow:indexpath.Row+1, Insection:indexpath. Section) tableview.insertrowsatindexpaths ([Zyindexpath], WithRowAnimation:UITableViewRowAnimation.Middle)} } func TableView (Tableview:uitableview, Editingstyleforrowatindexpath indexpath:nsindexpath), uitableviewcelled Itingstyle {if(Indexpath.Row%2==1) {return Uitableviewcelleditingstyle.Insert}Else{return Uitableviewcelleditingstyle.Delete} }
The two methods are then implemented. Look at the effect,
This can be added and deleted.
Next to retrieve this method
func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? { return ["第一组","第二组","第三组"] }
Click to use this method
UITableViewNSIndexPath) { print("我点击了第\(indexPath.section)部分,第\(indexPath.row)行") }
There are, of course, many ways. You can input TableView will prompt a lot, we can go to try. Don't repeat it here.
Or put on the source: UITableView Basic usage Daquan
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift detailed 19--------------UITableView basic Operations (dropdown refresh, add Delete, group, search, etc.)