Swift自定义UICollectionViewCell不显示的解决方案
2015-01-20 15:40
495 查看
今天遇到一个问题,用Swift自定义UICollectionViewCell,按照之前用OC的路子会出现cell不现实的问题。具体原因也不知道,可能是个Swift的bug吧,具体解决方法是用代码添加cell的子视图。而且这里注意,是在cell的contentView上添加子视图,这样就能显示出来了。
以下是我写的一个继承UICollectionViewCell的子类
而在nib中就什么也不要在拖拽子视图了,直接将cell 的clas修改为前面的CollectionViewCell就可以了
以下是我写的一个继承UICollectionViewCell的子类
import UIKit class CollectionViewCell: UICollectionViewCell { var imageView: UIImageView! var textLabel: UILabel! override init(frame: CGRect) { super.init(frame: frame) self.imageView = UIImageView(frame: CGRectMake(15, 10, 125, 120)) self.textLabel = UILabel(frame: CGRectMake(10, 135, 130, 21)) self.contentView.addSubview(self.imageView) self.contentView.addSubview(self.textLabel) } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) //self.imageView. //fatalError("init(coder:) has not been implemented") } }下面是代理方法
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { var cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewCell cell.textLabel?.text = "fsdfsafafsaf" cell.imageView?.backgroundColor = UIColor.redColor() return cell }
而在nib中就什么也不要在拖拽子视图了,直接将cell 的clas修改为前面的CollectionViewCell就可以了
相关文章推荐
- Swift之自定义UICollectionViewCell
- swift UICollectionView 代码创建 和自定义Cell
- iOS swift 自定义tableView不显示cell
- [IOS]swift自定义uicollectionviewcell
- UICollectionView cell点击后无法回调的解决方案
- swift uitableview显示cell最后一列
- 仿QQ空间用一个tableview显示多种自定义cell
- 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱
- 在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数
- UICollectionViewController xcode6.1 自定义Cell
- Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面
- UICollectionView学习<2>之UICollectionViewCell的自定义
- iOS UI 18 uicollectionview和自定义cell
- iOS UICollectionView 入门 05 创建自定义UICollectionViewCell
- iOS UI 18 uicollectionview和自定义cell
- Swift中tableViewCelll和ImageView中图片背景显示的问题
- UICollectionView 集合视图用法,自定义Cell
- IOS菜鸟的所感所思(四)——自定义UICollectionViewCell以及网络数据的添加。
- swift:自定义UICollectionViewFlowLayout
- Swift自定义UITableView、UICollectionView