Swift3.0 UICollectionView 删除,拖动
UICollectionView实现了一下常见的新闻分类. 附有效果图
近期一直在深入学习swift,实现了CollectionView item的头东与删除,用的都是系统的一些函数方法,看起来比较直观.
第一步:
class HotViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { //声明两个存放字符串的数组
var nowClassName = [String]()
var surplusClassName = [String]() //是否排序
var isRank = Bool() var collectionView : UICollectionView?
override func viewDidLoad() {
super.viewDidLoad() self.view.backgroundColor = ColorViewBG let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width:,height:)
//列间距,行间距,偏移
layout.minimumInteritemSpacing =
layout.minimumLineSpacing =
layout.sectionInset = UIEdgeInsetsMake(, , , ) collectionView = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: layout)
collectionView?.delegate = self
collectionView?.dataSource = self;
//注册一个cell
collectionView!.register(HotCell.self, forCellWithReuseIdentifier:"HotCell")
//注册区头
collectionView?.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "headView") collectionView?.backgroundColor = ColorViewBG
self.view.addSubview(collectionView!)
let gesture = UILongPressGestureRecognizer(target: self, action: #selector(viewCustom(_ :))) collectionView?.addGestureRecognizer(gesture) saveData()
}
func viewCustom(_ longPress:UILongPressGestureRecognizer){ let point:CGPoint = longPress.location(in: longPress.view) let indexPath = self.collectionView?.indexPathForItem(at: point) switch longPress.state {
case .began: self.collectionView?.beginInteractiveMovementForItem(at: indexPath!)
break
case .changed:
self.collectionView?.updateInteractiveMovementTargetPosition(point) break case .ended:
self.collectionView?.endInteractiveMovement()
break default:
self.collectionView?.cancelInteractiveMovement()
break
} }
//添加数据
private func saveData() {
nowClassName += ["A-1","A-2","A-3","A-4","A-5","A-6","A-7","A-8","A-9","A-10","A-11"]
surplusClassName += ["B-1","B-2","B-3","B-4","B-5","B-6","B-7","B-8","B-9","B-10","B-11"] }
// MARK: 代理
//每个区的item个数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if section == {
return nowClassName.count
}else { if !isRank {
return surplusClassName.count
}else{
return
}
} } //分区个数
func numberOfSections(in collectionView: UICollectionView) -> Int { if !isRank {
return
} return
} //自定义cell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HotCell", for: indexPath) as! HotCell
cell.backgroundColor = UIColor.red
if indexPath.section == {
cell.label.text = nowClassName[indexPath.item]
cell.button.addTarget(self, action: #selector(removeItem(_ :)), for: .touchUpInside)
cell.button.tag = indexPath.row cell.button.isHidden = !isRank }else{
cell.label.text = surplusClassName[indexPath.item]
cell.button.isHidden = true }
return cell } func removeItem(_ button:UIButton){ //执行在这里的时候,显示的是有个分区,否则崩溃,报不明错误!,研究过的可以告诉一下,不胜感激!!!
self.collectionView?.performBatchUpdates({
//数据变更
let item = self.nowClassName[button.tag]
self.nowClassName.remove(at: button.tag)
self.surplusClassName.append(item) let indexPath = IndexPath.init(item: button.tag, section: )
print(indexPath) let arr:[IndexPath] = [indexPath]
self.collectionView?.deleteItems(at: arr) }, completion: { (completion) in self.collectionView?.reloadData()
}) } //是否可以移动
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool { if isRank {
return true
}
return false } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//获取当前 item
//collectionView.cellForItem(at: indexPath)
//获取所有的item
//collectionView.indexPathsForVisibleItems if !isRank && indexPath.section == {
//先把数据更新好,因为移动后界面会自动刷新,否则会崩溃
nowClassName.append(surplusClassName[indexPath.item])
surplusClassName.remove(at: indexPath.item) let indexPath1 = NSIndexPath.init(item: nowClassName.count-, section: )
let indexPath2 = NSIndexPath.init(item: indexPath.item, section: ) //从当前位置移动到新的位置
collectionView.moveItem(at: indexPath2 as IndexPath, to: indexPath1 as IndexPath)
} } //设置拖动(手势拖动触发)
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { // if sourceIndexPath.section == 0 && destinationIndexPath.section == 0 {
// collectionView.exchangeSubview(at: sourceIndexPath.item, withSubviewAt: destinationIndexPath.item)
// } } //区头设置
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { //
//区头
var headerView : UICollectionReusableView? if kind == UICollectionElementKindSectionHeader{
headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headView", for: indexPath) //防止重用,这样很暴力
for view in (headerView?.subviews)!{
view.removeFromSuperview() } let label = UILabel.init(frame: CGRect(x:,y:,width:,height:))
if indexPath.section == {
label.text = "切换栏目" let button = UIButton.init(type: .custom)
button.frame = CGRect(x:collectionView.frame.size.width - ,y:,width:,height:)
button.titleLabel?.textColor = UIColor.cyan
button.backgroundColor = UIColor.white let str = isRank ? "完成排序" : "排序删除"
button.setTitle(str, for: .normal)
button.setTitleColor(UIColor.red, for: .normal)
headerView?.addSubview(button) button.addTarget(self, action: #selector(click(_ :)), for: .touchUpInside) }else if indexPath.section == {
label.text = "点击添加更多栏目"
}
headerView?.addSubview(label) } return headerView! } func click(_ btn:UIButton){ let str = isRank ? "完成排序" : "排序删除"
btn.setTitle(str, for: .normal) isRank = !isRank
print(isRank)
self.collectionView?.reloadData()
} //设置HeaderView的宽高
//MARK: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { return CGSize(width:collectionView.frame.size.width,height:) }
HotCell 类的实现
class HotCell: UICollectionViewCell { var label = UILabel()
var button = UIButton() override init(frame: CGRect) {
super.init(frame: frame) label = UILabel.init(frame: self.bounds)
label.textAlignment = .center
self.addSubview(label)
button = UIButton.init(type: .custom)
button.backgroundColor = UIColor.white
button.frame = CGRect(x:frame.size.width - ,y:,width:,height:)
self.addSubview(button) } required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} }
Swift3.0 UICollectionView 删除,拖动的更多相关文章
- Swift3.0 UICollectionView简单使用
感觉swift各版本语法改动太大,储备着吧
- Swift3.0语言教程删除字符与处理字符编码
Swift3.0语言教程删除字符与处理字符编码 Swift3.0语言教程删除字符 Swift3.0语言教程删除字符与处理字符编码,在字符串中,如果开发者有不需要使用的字符,就可以将这些字符删除.在NS ...
- UICollectionView在Swift3.0中的用法
UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UI ...
- iOS开发之资讯类App常用分类控件的封装与实现(CollectionView+Swift3.0+)
今天博客中,我们就来实现一下一些常用资讯类App中常用的分类选择的控件的封装.本篇博客中没有使用到什么新的技术点,如果非得说用到了什么新的技术点的话,那么勉强的说,用到了一些iOS9以后UIColle ...
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- Swift3.0语言教程使用路径字符串
Swift3.0语言教程使用路径字符串 Swift3.0语言教程使用路径字符串,路径其实是字符串的一种,我们称为路径字符串.本小节将讲解如何使用路径字符串. 1.组合路径 开发者可以将数组快速的组合成 ...
- Swift3.0语言教程替换子字符串
Swift3.0语言教程替换子字符串 Swift3.0语言教程替换子字符串,替换子字符串其实就是将字符串中的子字符串删除,然后再进行添加.为了让这一繁琐的过程变的简单,NSString提供了替换子字符 ...
- swift3.0 coredata 的使用
//swift3.0在语法上有很大的改变,以简单的增删改查为例,如下: //User类如下: import Foundation import CoreData extension User { @n ...
- Swift3.0相对于2.3语法的一些变化
前言 : Swift3.0的Swift的第3个主要版本,目标是安全,快速和有表现力,也是第一个有开源社区参与开发的Swift版本.由于语法和API改动比较多,Xcode 8.0 Beta提供了migr ...
随机推荐
- [转]Go基础之锁的初识
当我们的程序就一个线程的时候是不需要用到锁的,但是通常我们实际的代码不会是单个线程的,所有这个时候就需要用到锁了,那么关于锁的使用场景主要涉及到哪些呢? 当我们多个线程在读相同的数据的时候则是需要加锁 ...
- react 中的无状态函数式组件
无状态函数式组件,顾名思义,无状态,也就是你无法使用State,也无法使用组件的生命周期方法,这就决定了函数组件都是展示性组件,接收Props,渲染DOM,而不关注其他逻辑. 其实无状态函数式组件也是 ...
- AngularJs概述
- VS2013 update4+Cocos2d-x 3.7 Win8下安装方法及配置
1.安装VS 2013 update4 7个G.自己就去网上找吧,一大堆,密钥问度娘. 2.安装及配置python 2.x 这里注意,一定要下载python 3.0下面的版本号. 配置:进行环境变量配 ...
- 常用linux系统监控命令
一.内存监控 监控内存的使用状态是非常重要的,通过监控有助于了解内存的使用状态,比如内存占用是否正常,内存是否紧缺等等,监控内存最常使用的命令有free.vmstat.top等 1.1 free $ ...
- hibernate载入持久化对象的两种方式——get、load
一.get与load对照 在hibernate中get和load方法是依据id取得持久化对象的两种方法.但在实际使用的过程中总会把两者混淆,不知道什么情况下使用get好,什么时候使用load方法效率更 ...
- Tomcat 80端口 配置及域名访问步骤
一.修改端口tomcat默认监听端口是8080,我们如果想不带端口的直接访问项目,就必须监听80 端口: service.xml 以下代码段 <Connector port="8080 ...
- iOS APP第一次上架遇到的问题
现在苹果审核时越来越严了,我们有两个APP时同时上线的,代码用的也是一套的.但是有其中一个是第一次发布所以就拒了,信息就是下图.大概意思是用到支付了吗?用户是怎么来的.值需要把这些信息回复了.就OK ...
- wxpython中控件对键盘输入无响应的可能原因
问题描述: 开发环境:Win7 32bit + Python2.7.6 + WxPython 3.0.1-b20140707 开发某初级CAD软件中,需要实现点击TreeCtrl控件的相应选择,实现G ...
- EF(Entity Framework)通用DBHelper通用类,增删改查以及列表
其中 通用类名:DBhelper 实体类:UserInfo 1 //新增 2 DBHelper<UserInfo> dbhelper = new DBHelper<UserInfo& ...