python tableview添加内容_Swift - 给TableView添加编辑功能(删除,插入)

1,下面的样例是给表格UITableView添加编辑功能:

(1)给表格添加长按功能,长按后表格进入编辑状态

(2)在编辑状态下,第一个分组处于删除状态,第二个分组处于插入状态

(3)点击删除图标,删除对应条目

(4)点击添加图标,插入一条新数据

5819d62098010.png

import UIKit

class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {

var tableView:UITableView!

var allNames:Dictionary!

var addHeaders:[String]!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

allNames =

[

0:[String](["UILabel 标签", "UITextField 文本框", "UIButton 按钮"]),

1:[String](["UIDatePiker 日期选择器", "TableView 表格视图", "UIToolbar 工具条", "UIWebView 浏览器"])

]

print(allNames)

addHeaders = ["常见 UIKit 控件","高级 UIKit 控件"]

// 创建表格

tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.grouped)

tableView.delegate = self

tableView.dataSource = self

self.view .addSubview(tableView)

// 注册cell

tableView .register(UITableViewCell.self, forCellReuseIdentifier: "cell")

// 创建表头标签

let headerLabel = UILabel.init(frame: CGRect(x:0, y:20, width:375, height:30))

headerLabel.backgroundColor = UIColor.yellow

headerLabel.textColor = UIColor.red

headerLabel.numberOfLines = 0

headerLabel.lineBreakMode = .byWordWrapping

headerLabel.text = "高级 UIKit"

headerLabel.textAlignment = NSTextAlignment.center

headerLabel.font = UIFont.systemFont(ofSize: 15)

tableView.tableHeaderView = headerLabel

let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressAction))

longPress.delegate = self

longPress.minimumPressDuration = 1

tableView .addGestureRecognizer(longPress)

}

func longPressAction(recognizer:UILongPressGestureRecognizer) {

if recognizer.state == UIGestureRecognizerState.began {

print("UIGestureRecognizerStateBegan");

}

if recognizer.state == UIGestureRecognizerState.changed {

print("UIGestureRecognizerStateChanged");

}

if recognizer.state == UIGestureRecognizerState.ended {

print("UIGestureRecognizerStateEnded");

if tableView.isEditing == true {

tableView.isEditing = false

}

else

{

tableView.isEditing = true

}

}

}

// 创建分区

func numberOfSections(in tableView: UITableView) -> Int {

return allNames.count

}

// 每个分区的行数

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return allNames[section]!.count

}

// 分区头部显示

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

return addHeaders[section]

}

// 分区尾部显示

func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {

let data = allNames[section]

return "有\(data!.count)个控件"

}

// 显示cell内容

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let identify = "cell"

let secno = indexPath.section

let data = allNames[secno]

var cell = UITableViewCell()

cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: identify)

if secno == 0 {

cell.accessoryType = .disclosureIndicator

cell.textLabel?.text = data?[indexPath.row]

cell.imageView?.image = UIImage(named:"bug")

}

else

{

cell.textLabel?.text = data?[indexPath.row]

cell.detailTextLabel?.text = "\(data![indexPath.row])的详解"

}

return cell

}

// cell的选中事件

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

// 确定该分组的内容

let str = allNames[indexPath.section]?[indexPath.row]

print("str\(str)")

}

// 设置单元格的编辑的样式

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {

if indexPath.section == 0 {

return UITableViewCellEditingStyle.insert

}

else

{

return UITableViewCellEditingStyle.delete

}

}

// 设置确认删除按钮的文字

func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {

return "确认删除"

}

// 单元格编辑后的响应方法

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == UITableViewCellEditingStyle.delete {

self.allNames[indexPath.section]?.remove(at: indexPath.row)

tableView.setEditing(false, animated: true)

}

else if editingStyle == UITableViewCellEditingStyle.insert

{

allNames[indexPath.section]?.insert("插入的", at: indexPath.row)

tableView.setEditing(false, animated: true)

}

tableView.reloadData()

}

}

功能改进

(1)默认情况下所有单元格都无法进行滑动删除等编辑操作。

(2)长按表格进入编辑状态,所有单元格都可以进行删除操作。

(3)同时在编辑状态下,在下方会自动出现一个新增操作单元格。点击前面的加号,便会给数据集中添加一条新数据。

5819d525b834b.png

import UIKit

class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate {

var tableView:UITableView!

var allNames:Dictionary!

var addHeaders:[String]!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

allNames =

[

0:[String](["UILabel 标签", "UITextField 文本框", "UIButton 按钮"]),

1:[String](["UIDatePiker 日期选择器", "TableView 表格视图", "UIToolbar 工具条", "UIWebView 浏览器"])

]

print(allNames)

addHeaders = ["常见 UIKit 控件","高级 UIKit 控件"]

// 创建表格

tableView = UITableView.init(frame: self.view.frame, style: UITableViewStyle.grouped)

tableView.delegate = self

tableView.dataSource = self

self.view .addSubview(tableView)

// 注册cell

tableView .register(UITableViewCell.self, forCellReuseIdentifier: "cell")

// 创建表头标签

let headerLabel = UILabel.init(frame: CGRect(x:0, y:20, width:375, height:30))

headerLabel.backgroundColor = UIColor.yellow

headerLabel.textColor = UIColor.red

headerLabel.numberOfLines = 0

headerLabel.lineBreakMode = .byWordWrapping

headerLabel.text = "高级 UIKit"

headerLabel.textAlignment = NSTextAlignment.center

headerLabel.font = UIFont.systemFont(ofSize: 15)

tableView.tableHeaderView = headerLabel

let longPress = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressAction))

longPress.delegate = self

longPress.minimumPressDuration = 1

tableView .addGestureRecognizer(longPress)

}

func longPressAction(recognizer:UILongPressGestureRecognizer) {

if recognizer.state == UIGestureRecognizerState.began {

print("UIGestureRecognizerStateBegan");

}

if recognizer.state == UIGestureRecognizerState.changed {

print("UIGestureRecognizerStateChanged");

}

if recognizer.state == UIGestureRecognizerState.ended {

print("UIGestureRecognizerStateEnded");

if tableView.isEditing == true {

tableView.isEditing = false

}

else

{

tableView.isEditing = true

}

tableView.reloadData()

}

}

// 创建分区

func numberOfSections(in tableView: UITableView) -> Int {

return allNames.count

}

// 每个分区的行数

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

var count = allNames[section]!.count

if tableView.isEditing {

count += 1

}

return count

}

// 分区头部显示

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

return addHeaders[section]

}

// 分区尾部显示

func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {

let data = allNames[section]

return "有\(data!.count)个控件"

}

// 显示cell内容

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let identify = "cell"

let secno = indexPath.section

let data = allNames[secno]

var cell = UITableViewCell()

if secno == 0 {

cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: identify)

if tableView.isEditing && indexPath.row == data?.count {

cell.textLabel?.text = "添加新数据..."

}

else

{

cell.accessoryType = .disclosureIndicator

cell.textLabel?.text = data?[indexPath.row]

cell.imageView?.image = UIImage(named:"bug")

}

}

else if secno == 1

{

cell = UITableViewCell.init(style: UITableViewCellStyle.subtitle, reuseIdentifier: identify)

if tableView.isEditing && indexPath.row == data?.count {

cell.textLabel?.text = "添加新数据..."

}

else

{

cell.textLabel?.text = data?[indexPath.row]

cell.detailTextLabel?.text = "\(data![indexPath.row])的详解"

}

}

return cell

}

// cell的选中事件

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

// 确定该分组的内容

let str = allNames[indexPath.section]?[indexPath.row]

print("str\(str)")

}

// 设置单元格的编辑的样式

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {

if indexPath.section == 0 {

if tableView.isEditing == false {

return UITableViewCellEditingStyle.none

}

else if indexPath.row == allNames[indexPath.section]?.count {

return UITableViewCellEditingStyle.insert

}else {

return UITableViewCellEditingStyle.delete

}

}

else

{

if tableView.isEditing == false {

return UITableViewCellEditingStyle.none

}

else if indexPath.row == allNames[indexPath.section]?.count {

return UITableViewCellEditingStyle.insert

}else {

return UITableViewCellEditingStyle.delete

}

}

}

// 设置确认删除按钮的文字

func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {

return "确认删除"

}

// 单元格编辑后的响应方法

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == UITableViewCellEditingStyle.delete {

self.allNames[indexPath.section]?.remove(at: indexPath.row)

}

else if editingStyle == UITableViewCellEditingStyle.insert

{

allNames[indexPath.section]?.insert("插入的", at: indexPath.row)

}

tableView.reloadData()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值