UICollection查看单元格内容未显示

时间:2018-01-22 08:12:25

标签: ios swift uicollectionview uicollectionviewcell

enter image description here导入UIKit     导入FirebaseAuth

class SideMenuController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate,UICollectionViewDelegateFlowLayout 
 {

    var users = [User]()
    var newmessage: NewMessageController?
    struct userobject {
        var username:String
        var profilepicture:UIImage

    }
    var userArray: [userobject] = []

    @IBOutlet weak var sidecollectionview: UICollectionView!

    @IBOutlet weak var tableView: UITableView!
    struct PreviewDetail {
        let title: String
        let preferredHeight: Double
    }

    let sampleData:[String] = [
       "Trending Events","International Islamic University,Islamabad","Shaheed Zulfikar Ali Bhutto Institute of Science and Technology,Islamabad","Institute of Space Technology, Islamabad","Bahria University,Islamabad","Chat","CreateEvent","Signout"
    ]
    let sampleData1:[String] = [
        "Trending Events","International Islamic University,Islamabad","Shaheed Zulfikar Ali Bhutto Institute of Science and Technology,Islamabad","Institute of Space Technology, Islamabad","Bahria University Islamabad"
    ]



    override func viewDidLoad() {
        super.viewDidLoad()


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

        sidecollectionview.delegate = self
        sidecollectionview.dataSource = self
        userArray.append(userobject(username: "Farasat Niazi",profilepicture: #imageLiteral(resourceName: "images-8") ))
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

        // Do any additional setup after loading the view.
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return userArray.count
    }
    override func viewWillAppear(_ animated: Bool) {
        animateTable()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.size.width, height: 10.0)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        print("executing cell")
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "usercell" , for: indexPath)as! SideMenuCollectionViewCell
        cell.username.text = userArray[indexPath.item].username
        cell.imageview.image = userArray[indexPath.item].profilepicture
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of items in the sample data structure.

        var count:Int?

        if tableView == self.tableView {
            if Auth.auth().currentUser != nil {

            count = sampleData.count
            }
            else {
            count =  sampleData1.count
            }
        }


        return count!

    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell:UITableViewCell?

        if tableView == self.tableView {
            cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath as IndexPath)
            cell!.textLabel!.text = sampleData[indexPath.row]


        }

               return cell!
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("did select:      \(indexPath.row)  ")
    }

    func animateTable() {
        tableView.reloadData()

        let cells = tableView.visibleCells;        let tableHeight: CGFloat = tableView.bounds.size.height

        for i in cells {
            let cell: UITableViewCell = i as UITableViewCell
            cell.transform = CGAffineTransform(translationX: 0, y: tableHeight)
        }

        var index = 0

        for a in cells {
            let cell: UITableViewCell = a as UITableViewCell
            UIView.animate(withDuration: 1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: UIViewAnimationOptions(rawValue: UInt(0)) , animations: {
                cell.transform = CGAffineTransform(translationX: 0, y: 0);
                }, completion: nil)

            index += 1
        }
    }

 import UIKit

class SideMenuCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var signbutton: UIButton!
    @IBOutlet weak var username: UILabel!
    @IBOutlet weak var imageview: UIImageView!

}
import UIKit

class SideMenuLayoutCollectionView: UICollectionViewLayout {

}

嘿伙计们我的集合视图只显示其背景而不显示单元格内容。

我检查过的事情:

  • 将数据源和委托分配给集合视图。
  • 集合视图单元格标识符是" usercell"
  • 集合视图单元类附加在身份检查器中。

2 个答案:

答案 0 :(得分:1)

实施项目方法的大小

func collectionView(_ collectionView: UICollectionView,
                layout collectionViewLayout: UICollectionViewLayout,
                sizeForItemAt indexPath: IndexPath) -> CGSize {
// your code here
}

在viewDidLoad

collectionView.registerClass(SideMenuCollectionViewCell.self, forCellWithReuseIdentifier: "usercell")

答案 1 :(得分:0)

如果您已在情节提要或xib中配置了单元,请删除以下行,

collectionView.registerClass(SideMenuCollectionViewCell.self, forCellWithReuseIdentifier: "usercell")

对我有用。