`

swift -> UITableView 编辑模式

 
阅读更多

 

TableView 参考 :  http://mft.iteye.com/admin/blogs/2314641

 

class Do_bookmark: UIViewController,UITableViewDelegate,UITableViewDataSource {

    
  
    var tb:UITableView!;
    
    //var data:[[String:Any]]!;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // set nav bar
        self.title = "列表"
        let rightBtn = UIBarButtonItem(title: "编辑", style: .plain, target: self, action: #selector(Do_bookmark.navRightBtn));
        self.navigationItem.rightBarButtonItem = rightBtn
    
        //
        //let f:CTFrame = self.view.frame as! CTFrame;
        
        tb = UITableView(frame: CGRect(x: 0, y: 0,width: alert_width, height: alert_height));
        tb.delegate = self
        tb.dataSource = self;
        //进入编辑状态是否可以点击每行
        tb.allowsSelectionDuringEditing = true
        self.view.addSubview(tb);
        
        
    }
    
    
    
    func navRightBtn(){//根据Table 的编辑状态 显示 “编辑”还是“完成”
        if(tb.isEditing == true){
            tb.setEditing(false, animated: true);
            tb.isEditing = false;
            self.navigationItem.rightBarButtonItem?.title = "编辑";
        }else{
            tb.setEditing(true, animated: true)
            tb.isEditing = true;
            self.navigationItem.rightBarButtonItem?.title = "完成";
        }

         
    }
   
    
    var data = ["数据0","数据1","数据2","数据3","数据4","数据5","数据6","数据7","数据8","数据9","数据10","数据11"]
    //每行有几个区块
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    //多少行
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return data.count
        
    }

    //-------- 进入编辑模式 ---------    
    
    //设置cell 是否允许拖动排序
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        if(indexPath.row == 0){//如果是第一行就不允许拖动
            return false
        }else{
            return true
        }
    }
    
    //拖动排序 后  时 触发
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        //data.chan
        print("move row at")
        //data.exchangeObject(at: (sourceIndexPath as NSIndexPath).row, withObjectAt: (destinationIndexPath as NSIndexPath).row)
    }
    //进入编辑模式后每行的显示样式
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        if(indexPath.row == 0){//如果是第一行就不显示删除
            return UITableViewCellEditingStyle.none
        }else{
            return UITableViewCellEditingStyle.delete
        }
        
    }
    //对应行是否需要缩进
    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        if(indexPath.row == 0){//如果是第一行就不缩进
            return false
        }else{
            return true
        }
    }
    //增加左划显示删除, 
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        //如果点击了删除
        if(editingStyle == UITableViewCellEditingStyle.delete){
            print("you do del")
            
            // 修改数据源 必须 要 执行,否则 会出错
            data.remove(at: indexPath.row);
            //删除 表 视图 对应的行
            tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.top)
        }
    }
    
    //\\========= 进入编辑模式 ===============
    
    
    //点击每行执行
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if(indexPath.row == 0){
            self.navigationController?.pushViewController(Do_history(), animated: true)
        }
        
    }
    //设置分割线位移
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0);
     
    }
    
    // 开始往每行写内容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //---
     
        
        let cell = BookMarkViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "diy_cell") as BookMarkViewCell;
        //UITableViewCellAccessoryDisclosureIndicator;
       
        cell.title.text = data[indexPath.row];
        cell.logo.image = #imageLiteral(resourceName: "bookmark_icon")
       
        //去除点击后的显示灰色背景效果
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        
        return cell;
        //====
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        //
    }
    
    
    
    
    
}

 

 

分享到:
评论

相关推荐

    swift-UITableView数据源和代理封装极大的提高开发效率

    8. **编辑模式**:如果支持单元格的删除或移动,代理方法如`commitEditingStyle`和`moveRowAt`也需要在数据源中实现。 通过以上封装,我们能够创建一个可复用、可扩展的`CCTableDataSource`,适用于各种类型的表格...

    swift-一款针对于UITableview数据为空的加载视图

    本文将详细介绍`swift-一款针对于UITableView数据为空的加载视图`,以及如何使用它来增强用户体验。 首先,这个项目是用Swift语言编写的,主要针对`UITableView`的UI相关控件进行优化。它提供了一个轻量级、易于...

    UITableView 编辑状态下的批量选择与左滑删除等随笔

    本篇随笔将深入探讨`UITableView`的编辑模式,包括批量选择和左滑删除功能,这些都是提升用户体验的关键特性。 首先,我们来讨论批量选择功能。在`UITableView`中实现批量选择通常涉及到两种模式:单选和多选。单选...

    UITableView编辑-右划插入和左划删除、置顶、标记.zip

    Swift中的UITableView支持两种编辑模式:普通编辑模式(normal editing mode)和移动编辑模式(move editing mode)。在普通编辑模式下,用户可以通过左滑单元格进行删除操作;在移动编辑模式下,用户可以拖动单元格...

    UITableView删除功能(非编辑模式)更新版本

    本文将深入探讨如何在非编辑模式下实现UITableView的动态删除功能,并结合描述中的“提示删除”来创建一个更加用户友好的体验。 首先,让我们了解UITableView的基本操作。UITableView包含两种基本单元格类型:...

    swift-YJDropdownListView文本输入框下拉列表

    Swift-YJDropdownListView提供了这样的解决方案,它将UITextField(文本输入框)与UITableView(表格视图)相结合,使得用户在输入过程中能够看到与输入内容匹配的下拉选项。这种设计模式常见于各种应用,如搜索框、...

    swift-LXFWeChat用Swift3.0高仿微信

    在LXFWeChat项目中,开发者不仅会学习到Swift的基础语法,还会接触到诸如AutoLayout进行界面布局,MVVM(Model-View-ViewModel)设计模式来组织代码结构,以及如何有效地利用Swift的协议(Protocol)和扩展...

    swift-TableViewCell图文混排图片大小自适应

    对于"支持tableViewcell编辑插入、收藏、点赞、置顶、删除功能"这部分,这涉及到UITableView的编辑模式。Swift提供了内置的编辑支持,包括插入(insert)、删除(delete)操作。要添加收藏、点赞和置顶等功能,可能...

    swift-模仿鲨鱼记账iOS版

    - **NotificationCenter**:Swift中的观察者模式实现,用于组件间的通信。 - **Delegate**:定义协议并实现代理方法,处理视图间的交互。 7. **表视图与集合视图**: - **UITableView**和**UICollectionView**:...

    swift-类似微信朋友圈和内涵段子评论回复TextView自动变化高度使用简单

    在Swift编程中,实现类似微信朋友圈或内涵段子的评论回复功能,往往涉及到一个关键的UI组件:TextView。...这个过程涉及到Swift的基础知识,如类的继承、委托模式以及事件监听,也是iOS开发中常见的需求之一。

    swift-CommitMessageEditor一个小巧快速的commit消息编辑器

    Swift Commit Message Editor是一款专为Git提交消息编辑设计的轻量级工具,它旨在提供一个高效且用户友好的界面,帮助开发者快速、规范地编写commit消息。这款应用是用Swift编程语言开发的,充分展示了Swift在构建UI...

    UITableview在编辑状态下的批量选择与左滑删除等

    总之,通过`UITableView`的编辑模式和`UITableViewCellEditingStyle`,我们可以轻松实现批量选择和左滑删除功能,为用户带来便捷的操作方式。在实际开发中,这些功能能显著提高应用的易用性,提升用户体验。

    swift-基于Photos和AVFoundation框架开源相册多选与相机连拍

    本项目"swift-基于Photos和AVFoundation框架开源相册多选与相机连拍"旨在提供一个完整的解决方案,允许用户在应用中实现相册的多选功能以及相机的连拍模式。以下是关于这些框架和功能的详细解释: 1. **Photos框架*...

    uitableview使用

    `UITableView`支持编辑模式,如删除、插入和移动行。要启用编辑模式,需设置`isEditing`属性,并实现以下代理方法: - `tableView(_:commit:forRowAt:)`:处理编辑操作,如删除。 - `tableView(_:canEditRowAt:)`:...

    用 UITableView 进行多选的代码例子

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // 返回数据源数量 return dataSource.count } func tableView(_ tableView: UITableView, canEditRowAt indexPath: ...

    swift-XLForm是最灵活和最强大的iOS库用于创建动态表格视图

    XLForm的核心概念是基于表格视图(UITableView)的自定义单元格(UITableViewCell)。通过定义不同的单元格类型,开发者可以创建各种各样的输入字段,如文本框、选择器、开关、日期选择器等。这种灵活性使表单能够...

    UITableView

    了解这些基础知识后,你可以进一步探索更复杂的功能,如分组、自定义cell动画、可编辑模式等。在实际项目中,结合Storyboard或SwiftUI进行界面设计会更加高效,但理解纯代码实现对于开发者来说仍然非常重要。

    UITableView:隐藏删除按钮和设置删除按钮的标题

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { return false } ``` 2. **设置删除按钮标题** 若要自定义删除按钮的标题,我们可以使用`...

    iOS编辑模式

    本教程将深入探讨如何利用`UITableView`的编辑模式来实现自定义可编辑的单元格(cell),以及如何通过nib( nib文件,即 Interface Builder 中的设计文件)来辅助这个过程。 首先,让我们了解nib文件的作用。在iOS...

    swift-TableView

    通过设置`tableView.setEditing(_:animated:)`进入或退出编辑模式。 总之,Swift语言为iOS开发者提供了便捷的方式来创建和操作UITableView。理解并熟练掌握上述知识点,能帮助你构建功能丰富的数据展示界面,提升...

Global site tag (gtag.js) - Google Analytics