关键词:
【中文标题】使用 Swift 的 UICollectionView 错误【英文标题】:UICollectionView error using Swift 【发布时间】:2014-06-12 07:58:17 【问题描述】:我收到此错误,尝试在 Swift 中使用 UICollectionView:
NSInternalInconsistencyException', reason: 'attempt to register a cell class which is not a subclass of UICollectionViewCell ((null))
但我想我正在注册单元格:
ViewDidLoad:
override func viewDidLoad()
super.viewDidLoad()
self.collectionView.registerClass(NSClassFromString("CollectionCell"),forCellWithReuseIdentifier:"CELL")
cellForItemAtIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as CollectionCell
cell.titleLabel.text="cellText"
return cell
和细胞类:
class CollectionCell: UICollectionViewCell
@IBOutlet var titleLabel : UILabel
init(coder aDecoder: NSCoder!)
super.init(coder: aDecoder)
任何帮助表示赞赏
【问题讨论】:
【参考方案1】:您需要以 Swift 风格将 UICollectionViewCell 的子类传递给 registerClass:
self.collectionView.registerClass(CollectionCell.self, forCellWithReuseIdentifier:"CELL")
【讨论】:
谢谢!进展,现在我得到一个“使用未实现的初始化程序'init(frame :)”,但我到了那里! 这解决了.. init(frame: CGRect) super.init(frame: frame) 再次感谢。【参考方案2】:如果您没有使用任何自定义类,只需在 ViewDidLoad 中使用
myCollectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
【讨论】:
【参考方案3】:myCollectionView!.registerClass(UICollectionViewCell.classForCoder(), forCellWithReuseIdentifier: "cellID")
是推荐的代码。为我工作。希望对您有所帮助。
【讨论】:
【参考方案4】:对于您的细胞:
class CollectionCell: UICollectionViewCell
@IBOutlet var titleLabel : UILabel
init(coder aDecoder: NSCoder!)
super.init(coder: aDecoder)
对于您的 ViewController:
import UIKit
class NextViewController: UIViewController
@IBOutlet var collectionView : UICollectionView
var ListArray=NSMutableArray()
override func viewDidLoad()
super.viewDidLoad()
var nipName=UINib(nibName: "GalleryCell", bundle:nil)
collectionView.registerNib(nipName, forCellWithReuseIdentifier: "CELL")
for i in 0..70
ListArray .addObject("C: \(i)")
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section:Int)->Int
return ListArray.count
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath:NSIndexPath)->UICollectionViewCell
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("CELL", forIndexPath: indexPath) as GalleryCell
cell.titleLabel.text="\(ListArray.objectAtIndex(indexPath.item))"
return cell
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
return CGSizeMake(66, 58)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【讨论】:
谢谢,但我更喜欢使用 registerClass 而不是 registerNib。 但是下次我会保留这个.. +1【参考方案5】:试试这个:
self.collectionView.registerClass(CollectionViewCell.self,forCellWithReuseIdentifier:"CELL")
【讨论】:
感谢您的回答。按照您的建议进行更改会导致以下控制台输出: 0x1001bffeb: leaq 0x16067(%rip), %rax ; “Swift 动态转换失败” 0x1001bfff2: movq %rax, 0x77c67(%rip) ; gCRAnnotations + 8 0x1001bfff9: int3 0x1001bfffa: nopw (%rax,%rax)swift使用简体中文的swift词典(代码片段)
Swift:使用 UITableView 的数组大小
】Swift:使用UITableView的数组大小【英文标题】:Swift:ArraySizewithUITableView【发布时间】:2016-04-0210:14:33【问题描述】:我试图弄清楚数组如何与Swift一起工作。我了解您使用let创建一个不可变数组并使用var创建一个可变数组。然而... 查看详情
使用 Swift 的 UICollectionView 错误
】使用Swift的UICollectionView错误【英文标题】:UICollectionViewerrorusingSwift【发布时间】:2014-06-1207:58:17【问题描述】:我收到此错误,尝试在Swift中使用UICollectionView:NSInternalInconsistencyException\',reason:\'attempttoregisteracellclasswhichisnotasubc... 查看详情
swift在swift中使用self的协议(代码片段)
使用 UICollectionView 和 MPMediaItemArtwork 遇到内存泄漏
】使用UICollectionView和MPMediaItemArtwork遇到内存泄漏【英文标题】:ExperiencingmemoryleakusingUICollectionViewandMPMediaItemArtwork【发布时间】:2016-08-0700:33:35【问题描述】:我正在构建一个基本的音乐播放器应用程序,利用Apple的MediaPlayer访问... 查看详情
swift中代理的使用
下面以自定义的UITableViewCell的代理为例,记录一下swift中代理的使用controller中的代码如1//2//ViewController.swift3//simpleDemo4//5//Createdbyliuboon16/7/25.6//Copyright©2016年liubo.Allrightsreserved.7//89importUIKit1011classV 查看详情
使用 AFNetworking 的 Swift 3
】使用AFNetworking的Swift3【英文标题】:Swift3usingAFNetworking【发布时间】:2016-12-0906:53:28【问题描述】:我正在将AFNetworking与Swift3.0一起使用,但我被困在一个代码上。funcgetJSON()letmanager=AFHTTPSessionManager()manager.get(url,parameters:nil,succes... 查看详情
使用 Swift 启动应用程序的次数?
】使用Swift启动应用程序的次数?【英文标题】:CountnumberoftimesapphasbeenlaunchedusingSwift?【发布时间】:2015-08-1213:35:22【问题描述】:我想计算我的iOS应用程序使用Swift启动的次数。然后我想获取该号码并每次使用NSLog显示它。【问... 查看详情
项目里面swift和oc交叉使用
在OC的项目中使用Swift语言开发创建swift文件,同时创建桥接文件。(桥接文件里面不用导入头文件)在swift文件中完成代码的编写。在某OC类的.m文件中,使用swift文件。方法;#import"OC工程的产品名-Swift.h"<固定的,不要乱写>头文... 查看详情
masonry在swift下的使用
Masonry在oc下使用很方便,但是在swift下,由于oc方法和property都可以使用.fuc的语法,swift下只有属性可以使用.property的语法,方法只能写成.func().因此在swift下如果直接写就只能写成downTriangle.mas_makeConstraints{(make:MASConstraintMaker?)in... 查看详情
swift在swift中使用特征和协议的演示。(代码片段)
Swift 中下标的模糊使用
】Swift中下标的模糊使用【英文标题】:AmbiguousUseofSubscriptinSwift【发布时间】:2016-02-1201:01:49【问题描述】:我的Swift代码中不断出现“下标使用不明确”的错误。我不知道是什么导致了这个错误。它只是随机弹出。这是我的代... 查看详情
使用swift的临时文件路径
】使用swift的临时文件路径【英文标题】:Temporaryfilepathusingswift【发布时间】:2015-09-1817:09:17【问题描述】:如何在OSX上使用Swift/Cocoa获取唯一的临时文件路径?Cocoa似乎没有为此提供函数,只有NSTemporaryDirectory()返回临时目录的... 查看详情
使用 Swift 的预期声明错误
】使用Swift的预期声明错误【英文标题】:ExpectedDeclarationErrorusingSwift【发布时间】:2015-04-2322:29:23【问题描述】:我正在尝试使用NSUserDefaults将UISwitch的布尔值传递给另一个类。出于某种原因,在包含开关的类中,应该将值设置... 查看详情
在 swift 项目中集成 swift 框架,都使用 cocoapods
】在swift项目中集成swift框架,都使用cocoapods【英文标题】:Integrationswiftframeworkinswiftproject,bothusingcocoapods【发布时间】:2015-06-2913:12:56【问题描述】:我有一个使用cocoapods的cocoatouch框架和一个使用cocoapods的swift项目(框架中的一... 查看详情
如何使用 Swift Package Manager `swift build` 命令构建 Swift Package 的优化版本
】如何使用SwiftPackageManager`swiftbuild`命令构建SwiftPackage的优化版本【英文标题】:HowtobuildoptimizedversionofSwiftPackageusingtheSwiftPackageManager`swiftbuild`command【发布时间】:2017-02-0802:41:27【问题描述】:我想将以新的Swift包管理器格式编写... 查看详情
使用 Swift 2.3 编译的模块无法在 Swift 3.0 中导入
】使用Swift2.3编译的模块无法在Swift3.0中导入【英文标题】:ModulecompiledwithSwift2.3cannotbeimportedinSwift3.0【发布时间】:2016-09-1407:22:31【问题描述】:我将FacebookSDK(Swift)添加到我的项目中。现在我更新了Xcode8和Swift3。我的构建时间有... 查看详情