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 删除,拖动的更多相关文章

  1. Swift3.0 UICollectionView简单使用

    感觉swift各版本语法改动太大,储备着吧

  2. Swift3.0语言教程删除字符与处理字符编码

    Swift3.0语言教程删除字符与处理字符编码 Swift3.0语言教程删除字符 Swift3.0语言教程删除字符与处理字符编码,在字符串中,如果开发者有不需要使用的字符,就可以将这些字符删除.在NS ...

  3. UICollectionView在Swift3.0中的用法

    UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UI ...

  4. iOS开发之资讯类App常用分类控件的封装与实现(CollectionView+Swift3.0+)

    今天博客中,我们就来实现一下一些常用资讯类App中常用的分类选择的控件的封装.本篇博客中没有使用到什么新的技术点,如果非得说用到了什么新的技术点的话,那么勉强的说,用到了一些iOS9以后UIColle ...

  5. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  6. Swift3.0语言教程使用路径字符串

    Swift3.0语言教程使用路径字符串 Swift3.0语言教程使用路径字符串,路径其实是字符串的一种,我们称为路径字符串.本小节将讲解如何使用路径字符串. 1.组合路径 开发者可以将数组快速的组合成 ...

  7. Swift3.0语言教程替换子字符串

    Swift3.0语言教程替换子字符串 Swift3.0语言教程替换子字符串,替换子字符串其实就是将字符串中的子字符串删除,然后再进行添加.为了让这一繁琐的过程变的简单,NSString提供了替换子字符 ...

  8. swift3.0 coredata 的使用

    //swift3.0在语法上有很大的改变,以简单的增删改查为例,如下: //User类如下: import Foundation import CoreData extension User { @n ...

  9. Swift3.0相对于2.3语法的一些变化

    前言 : Swift3.0的Swift的第3个主要版本,目标是安全,快速和有表现力,也是第一个有开源社区参与开发的Swift版本.由于语法和API改动比较多,Xcode 8.0 Beta提供了migr ...

随机推荐

  1. 一致性hash算法在memcached中的使用

    一.概述 1.我们的memcacheclient(这里我看的spymemcache的源代码).使用了一致性hash算法ketama进行数据存储节点的选择.与常规的hash算法思路不同.仅仅是对我们要存 ...

  2. Android 最新控件 Toolbar

    之前Android的ActionBar好像做项目从没用过.除了google自己,并没有多少人用,究其原因,主要是由于ActionBar不够灵活,不可以随心所欲的定制,后来Goole也发现了这一点,然后 ...

  3. 为Joomla 2.5的连续插入多幅图像添加便捷方式

    用过Joomla 2.5的朋友应该都知道插入很多图像时是比較麻烦的.点了文章以下的图片button,它会弹出个div,让你选择图片,每选一张.div就关闭. 再选第二张的时候,它又要你又一次选择文件夹 ...

  4. Task C# 多线程和异步模型 TPL模型 【C#】43. TPL基础——Task初步 22 C# 第十八章 TPL 并行编程 TPL 和传统 .NET 异步编程一 Task.Delay() 和 Thread.Sleep() 区别

    Task C# 多线程和异步模型 TPL模型   Task,异步,多线程简单总结 1,如何把一个异步封装为Task异步 Task.Factory.FromAsync 对老的一些异步模型封装为Task ...

  5. C++ string string string string string string string string string string

    一. 初始化 string s1="i love you"; string s2(s1); //把s2初始化为string s1,注意不能写成string s2; s2(s1); ...

  6. ActiveMQ(五) 转

    package pfs.y2017.m11.mq.activemq.demo05; import javax.jms.Connection; import javax.jms.ConnectionFa ...

  7. yum lock

    状态 :睡眠中,进程ID:18439Another app is currently holding the yum lock; waiting for it to exit... 另一个应用程序是: ...

  8. 端口扫描 开启 防火墙 iptables SELinux

    Linux 如何打开端口 - lclc - 博客园 https://www.cnblogs.com/lcword/p/5869522.html linux如何查看端口相关信息_百度经验 https:/ ...

  9. Axure Base 07 元件使用思路的补充

    我们曾经对axure线框图基本元件进行过说明,现结合这我对这些元件的使用习惯,对部分元件的使用,再做一些补充. 1. 图片:可以编辑悬停.按下时候显示不同的图片,做图片的一些特殊效果. 2. 文本(l ...

  10. Hibernate commit() 和flush() 的区别

    <<精通Hibernate java对象持久化技术详解>> ,flush()方法进行清理缓存的操作,执行一系列的SQL语句,但不会提交事务;commit()方法会先调用flus ...