当我尝试允许用户创建自己的 uicollectionviewcell 时,iOS 模拟器中没有出现任何内容

Posted

技术标签:

【中文标题】当我尝试允许用户创建自己的 uicollectionviewcell 时,iOS 模拟器中没有出现任何内容【英文标题】:Nothing is appearing in the iOS simulator when I tried to allow users to create their own uicollectionviewcell 【发布时间】:2017-03-09 19:18:53 【问题描述】:

我的代码正在完美构建,我正在尝试使用模拟器,但除了空白屏幕之外什么都没有出现。我试图让用户创建自己的带有标题和图像的 uicollectionviewcell。我有一个 ChatRoom 视图控制器,它具有初始入口点,它还有一个导航控制器 segue 作为根视图控制器连接。我还有一个用于 UicollectionViewcell 的子类 ChatCell。我怎样才能解决这个问题 ?这是我的项目的github:https://github.com/fboch25/CollectionView2

//子类

class ChatCell: UICollectionViewCell 

@IBOutlet weak var chatLabel: UILabel!
@IBOutlet weak var chatImage: UIImageView!


// 原始视图控制器

class ChatRoom: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate 



@IBOutlet weak var collectionView2: UICollectionView!



var secondClass : ChatCell?

struct Object 
    var image: UIImage!
    var title: String!


// Properties
var object: Object?
var objects: [Object] = []
var picker: UIImagePickerController!


// Do any additional setup after loading the view, typically from a nib.


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


override func viewDidLoad() 
    super.viewDidLoad()

    picker = UIImagePickerController()
    picker?.allowsEditing = false
    picker?.delegate = self
    picker?.sourceType = .photoLibrary
    // NavigationBar Characteristics


override func viewDidAppear(_ animated: Bool) 
    self.navigationController?.isNavigationBarHidden = false
    self.navigationController?.navigationBar.barTintColor = UIColor.black
    self.navigationController?.navigationBar.isTranslucent = false
    // self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
    self.navigationItem.leftBarButtonItem?.tintColor = UIColor.green
    self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+", style: .plain, target: self, action: #selector(didSelectCreateButton))
    self.navigationItem.rightBarButtonItem?.tintColor = UIColor.green




func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as UICollectionViewCell
    let object = objects[indexPath.row]

    secondClass?.chatLabel.text = object.title ?? ""
    secondClass?.chatImage.image = object.image ?? UIImage()


    return cell


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
    return objects.count



func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 

    switch info[UIImagePickerControllerOriginalImage] as? UIImage 
    case let .some(image):
        object?.image = image
    default:
        break
    

    picker.dismiss(animated: true) 
        self.showCellTitleAlert()
    


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 

    object = nil

    dismiss(animated: true) 
        self.collectionView2!.reloadData()
    


// Alerts

func showCellTitleAlert() 

    let alert = UIAlertController(title: "Cell Title", message: nil, preferredStyle: .alert)

    alert.addTextField  $0.placeholder = "Title" 
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel)  _ in
        self.object = nil
    )
    alert.addAction(UIAlertAction(title: "Save", style: .default)  _ in
        self.object?.title = (alert.textFields?.first.flatMap  $0.text )!
        self.object.flatMap  self.objects.append($0) 
        self.collectionView2?.reloadData()
    )

    present(alert, animated: true, completion: nil)


// Create new Cell
////////?//////////////
@IBAction func didSelectCreateButton() 

    object = Object()

    present(picker, animated: true, completion: nil)

【问题讨论】:

我建议你给你的collectionView添加一个虚拟的背景颜色,看看它是否会改变。如果不是,那么更深层次的东西是错误的...... 【参考方案1】:

在你的 viewDidLoad 中添加这两行

collectionView2.delegate = self
collectionView2.dataSource = self

对于您的特定项目,我会进行两项更改。将右上角文本中的按钮更改为白色,以便您可以看到它。两个更改此方法,您将开始看到您的单元格。不敢相信我没有注意到上面的代码..

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ChatCell
    let object = objects[indexPath.row]

    cell.chatLabel.text = object.title ?? ""
    cell.chatImage.image = object.image ?? UIImage()
    return cell

【讨论】:

感谢您的帮助,当我把它放在 viewdidload 中时,它说 UICollectionview 没有“数据源” 它仍然什么也没做,我不确定发生了什么 我可以压缩我的项目吗? @KimRiegel 您可以将您的项目上传到 github 并在您的问题中分享其链接...... @Honey 是的,我可以

以上是关于当我尝试允许用户创建自己的 uicollectionviewcell 时,iOS 模拟器中没有出现任何内容的主要内容,如果未能解决你的问题,请参考以下文章

为啥 MongoDB 允许创建相同的用户? [复制]

如何允许用户在我的 MVC 网站上创建自己的子域? [复制]

允许其他用户执行 google 电子表格脚本

IAM允许用户仅查看自己的实例

Facebook登录错误“不允许用户查看该应用程序。”

创建基于渐变的 UICollectionViewCell 背景颜色