Swift全解析

文章26 |   阅读 15634 |   点赞0

来源:https://blog.csdn.net/u010586842/category_9264386.html

swift详解之十九--------------UITableView的基本操作(下拉刷新,新增删除,分组,检索等)

x33g5p2x  于2022-03-08 转载在 其他  
字(4.5k)|赞(0)|评价(0)|浏览(566)

UITableView的基本操作(下拉刷新,新增删除,分组,检索等)

注:本小结总结UITableview的一些基本用法

UITbleView继承自UIScrollView,只能用来显示一列数据(目前就只认识到这里),纵向滑动。
一般有两种方式来实现,直接用UITableViewController , 占满整个屏幕 。不用手动实现UITableViewDataSourceUITableViewDelegate 。另一种方式在UIViewController 中。我们看看这种方式

  1. let table = UITableView()
  2. table.frame = self.view.frame
  3. self.view = table
  4. table.delegate = self
  5. table.dataSource = self

这里新建了一个UITableView ,并且将其frame设置成当前view的frame大小 。也就是占满屏幕 。当然这里 你也可以设置你需要的大小, 下面的两句就是将它的代理和数据源的协议 设置成当前对象 当然我们的viewcontroller是实现了这两个协议的。class ViewController: UIViewController , UITableViewDataSource , UITableViewDelegate

然后,把这个tableview添加到当前view上 或者直接把当前view赋值成它。
下面看几个主要的代理方法

  1. //返回多少个section
  2. func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  3. return 3
  4. }

返回section数, section相当于分组 ,也就是这里table分成几组 。我们这里分三组演示。

  1. func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  2. switch section {
  3. case 0 :
  4. return arr.count
  5. case 1:
  6. return arr1.count
  7. case 2:
  8. return arr2.count
  9. default:
  10. return 0
  11. }
  12. }

然后这个方法就是每组的元素个数 。这里我们用了三个数组

  1. func initData(){
  2. arr.addObject("ssssddd")
  3. arr.addObject("唱什么")
  4. arr.addObject("what ")
  5. arr.addObject("ssshenme dd")
  6. arr1.addObject("我是二部的")
  7. arr2.addObject("我是第三个部门的")
  8. arr2.addObject("我是第三个部门的1")
  9. }

我们在这里初始化了这三个数组 。在viewDidLoad 下调用。

  1. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  2. let strCell = "cell"
  3. var cell = tableView.dequeueReusableCellWithIdentifier(strCell)
  4. if cell == nil{
  5. cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: strCell)
  6. }
  7. switch indexPath.section{
  8. case 0 :
  9. cell!.textLabel?.text = arr[indexPath.row] as? String
  10. case 1:
  11. cell!.textLabel?.text = arr1[indexPath.row] as? String
  12. case 2:
  13. cell!.textLabel?.text = arr2[indexPath.row] as? String
  14. default:
  15. cell!.textLabel?.text = ""
  16. }
  17. //cell!.textLabel?.text = arr[indexPath.row] as? String
  18. cell!.backgroundColor=UIColor.purpleColor()
  19. cell?.contentView.backgroundColor = UIColor.grayColor()
  20. return cell!
  21. }

这个也是最主要的方法 ,给cell赋值 。这里先从tableView.dequeueReusableCellWithIdentifier 这个里面去取 , 没有的时候 才会去新建 ,因为下载过的数据是回缓存起来的。我们需要先从缓存池里面找。找不到采取新建,这样会比较流畅。

  1. var refreshControl:UIRefreshControl?

这里我们声明一个刷新控件

  1. self.refreshControl = UIRefreshControl()
  2. self.refreshControl?.addTarget(self, action: "onPullToFresh", forControlEvents: UIControlEvents.ValueChanged)
  3. self.refreshControl?.attributedTitle=NSAttributedString(string: "松手就可以刷新啦")
  4. self.table.addSubview(refreshControl!)

这里设置下舒心控件的属性和给它注册方法onPullToFresh

  1. func onPullToFresh(){
  2. //下拉刷新
  3. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
  4. self.arr.addObject("ssssddd")
  5. self.arr1.addObject("唱什么")
  6. self.arr2.addObject("what ")
  7. self.arr1.addObject("ssshenme dd")
  8. dispatch_async(dispatch_get_main_queue(), {
  9. self.table.reloadData()
  10. self.refreshControl?.endRefreshing()
  11. })
  12. }
  13. }

这个下拉刷新,先去异步添加数据 ,我们这里是手动添加进去的,但是实际应用中往往需要从网络去下载数据,所以比较慢,用异步会比较合适。下载完成后,回到主线程去重新加载数据 , 最后停止下拉刷新控件 。

  1. table.editing = true

这里我们设置这个属性后, tableview可以编辑 , 新增删除等 。(不加这个属性只能左划删除 )

  1. func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
  2. if(editingStyle == UITableViewCellEditingStyle.Delete){ //delete
  3. switch indexPath.section {
  4. case 0 :
  5. arr.removeObjectAtIndex(indexPath.row)
  6. case 1:
  7. arr1.removeObjectAtIndex(indexPath.row)
  8. case 2:
  9. arr2.removeObjectAtIndex(indexPath.row)
  10. default:
  11. print("no")
  12. }
  13. tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
  14. }else if(editingStyle == UITableViewCellEditingStyle.Insert){
  15. switch indexPath.section {
  16. case 0 :
  17. arr.insertObject("新增的❤️", atIndex: indexPath.row+1)
  18. case 1:
  19. arr1.insertObject("新增的❤️", atIndex: indexPath.row+1)
  20. case 2:
  21. arr2.insertObject("新增的❤️", atIndex: indexPath.row+1)
  22. default:
  23. print("no")
  24. }
  25. let zyIndexPath = NSIndexPath (forRow: indexPath.row+1, inSection: indexPath.section)
  26. tableView.insertRowsAtIndexPaths([zyIndexPath], withRowAnimation: UITableViewRowAnimation.Middle)
  27. }
  28. }
  29. func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
  30. if(indexPath.row % 2 == 1){
  31. return UITableViewCellEditingStyle.Insert
  32. }
  33. else{
  34. return UITableViewCellEditingStyle.Delete
  35. }
  36. }

然后再实现这两个方法。看下效果 、

这里可以新增和删除 。

旁边的检索这个方法

  1. func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
  2. return ["第一组","第二组","第三组"]
  3. }

点击可以用这个方法

  1. func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  2. print("我点击了第\(indexPath.section)部分,第\(indexPath.row)行")
  3. }

当然还有很多方法。大家可以输入tableview 会提示很多,大家可以去试试。这里不再赘述。

还是放上源码 :UITableView基本用法大全
学习iOS,有他就够了,小码哥视频,传智、黑马、各种swift书籍

相关文章