前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS UITableViewCell的删除和插入

IOS UITableViewCell的删除和插入

作者头像
用户5760343
发布2019-07-08 11:52:26
8890
发布2019-07-08 11:52:26
举报
文章被收录于专栏:sktj
代码语言:javascript
复制
----------------------------------------插入-------------------------------------------
 1 import UIKit
 2
 3 class ViewController:UIViewController,
 UITableViewDataSource, UITableViewDelegate{
 4
 5 var diablo3Level = [“普通模式”, “困难模式”, “高手模
 式”, “大师模式”, “地狱模式”]
 6
 7 override func viewDidLoad() {
 8 super.viewDidLoad()
 9 // Do any additional setup after loading the view,
 typically from a nib.
 10
 11 let screenRect = UIScreen.main.bounds
 12 let tableRect = CGRect(x:0, y:20, width:
 screenRect.size.width, height:screenRect.size.height - 20)
 13 let tableView = UITableView(frame:tableRect)
 14
 15 tableView.dataSource = self
 16 tableView.delegate = self
 17 tableView.setEditing(true, animated:true)
 18 self.view.addSubview(tableView)
 19 }
 20
 21 func tableView(_ tableView:UITableView,
 numberOfRowsInSection section:Int) -> Int{
 22 return diablo3Level.count
 23 }
 24
 25 func tableView(_ tableView:UITableView,
 cellForRowAt indexPath:IndexPath)
 26 -> UITableViewCell {
 27
 28 let identifier = “reusedCell”
 29 var cell =
 tableView.dequeueReusableCell(withIdentifier:identifier)
 30
 31 if(cell == nil){
 32 cell = UITableViewCell(style:
 UITableViewCellStyle.default,
 33 reuseIdentifier:identifier)
 34 }
 35
 36 cell?.textLabel?.text = diablo3Level[(indexPath as
 NSIndexPath).row]
 37 return cell!
 38 }
 39
 40 func tableView(_ tableView:UITableView,
 editingStyleForRowAt indexPath:IndexPath) ->
 UITableViewCellEditingStyle {
 41 return UITableViewCellEditingStyle.insert
 42 }
 43
 44 func tableView(_ tableView:UITableView, commit
 45 editingStyle:UITableViewCellEditingStyle, forRowAt
 indexPath:IndexPath) {
 46 if editingStyle == UITableViewCellEditingStyle.insert{
 47 diablo3Level.insert(“痛苦模式”, at:indexPath.row)
 48 tableView.insertRows(at:[indexPath], with:
 UITableViewRowAnimation.right)
 49
 50 }
 51 }
 52 }
//UITableViewCellEditingStyle

image.png

image.png

代码语言:javascript
复制
----------------------------------------------------删除--------------------------------
 接着将47~50行的代码修改为
 47 if editingStyle ==
 UITableViewCellEditingStyle.delete{
 48 diablo3Level.remove(at:indexPath.row)
 49 tableView.deleteRows(at:[indexPath], with:
 UITableViewRowAnimation.right)
 50 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.06.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档