我正在构建一个应用程序并希望允许用户导航到文件夹

问题描述 投票:0回答:0

用户最初会看到一个 ViewController,其中包含一组可供选择的文件夹。选择文件夹后,会出现一个名为 FolderContentsViewController 的新视图控制器,它应该显示文件夹的内容,但没有显示任何内容。 FolderContentsViewController 中有一个集合视图,其中包含名为“HoldGroupCell”的单元格。

这是我当前的 HoldGroupCell 代码和 FolderContentsViewController 的 UICollectionViewDataSource。有人可以帮我找到问题吗?

class HoldGroupCell: UICollectionViewCell {
    
    var folderURL: URL?

    let folderImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()
    
    let folderNameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = .black
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(folderImageView)
        contentView.addSubview(folderNameLabel)
        
        NSLayoutConstraint.activate([
            folderImageView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
            folderImageView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: -10),
            folderImageView.widthAnchor.constraint(equalToConstant: 50),
            folderImageView.heightAnchor.constraint(equalToConstant: 50),
            
            folderNameLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            folderNameLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            folderNameLabel.topAnchor.constraint(equalTo: folderImageView.bottomAnchor, constant: 5),
            folderNameLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
        ])
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configure(with itemURL: URL) {
        folderNameLabel.text = itemURL.lastPathComponent
        
        if itemURL.hasDirectoryPath {
            let folderImageName = itemURL.lastPathComponent
            let folderImage = UIImage(named: folderImageName)
            folderImageView.image = folderImage ?? UIImage(named: folderImageName)
        }
    }
    
    func configureContents(of folderURL: URL) -> [HoldGroupCell] {
        guard let contents = try? FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: []) else {
            return []
        }
        
        var cells: [HoldGroupCell] = []
        contents.forEach { (itemURL) in
            let itemCell = HoldGroupCell()
            itemCell.configure(with: self.folderURL!.appendingPathComponent(itemURL.lastPathComponent))
            cells.append(itemCell)
        }
        
        return cells
    }
}





extension FolderContentsViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return folderContents.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HoldGroupCell", for: indexPath) as! HoldGroupCell
        let fileURL = folderContents[indexPath.item]
        cell.backgroundColor = .lightGray

        var isDir: ObjCBool = false
        fileManager.fileExists(atPath: fileURL.path, isDirectory: &isDir)
        
        if isDir.boolValue {
            cell.backgroundColor = .gray
            
            let folderName = fileURL.lastPathComponent
            cell.folderNameLabel.text = folderName
            
            let folderImageName = folderName
            let folderImage = UIImage(named: folderImageName)
            cell.folderImageView.image = folderImage
            
            let cells = cell.configureContents(of: fileURL)
            cells.forEach { cell.contentView.addSubview($0) }
        }
        
        return cell
    }
ios swift uicollectionview uikit subdirectory
© www.soinside.com 2019 - 2024. All rights reserved.