2016-08-12 7 views
2

プログラミングに関してはかなり新しいです。私はスウィフトに書いています。私はUIcollectionViewです。あなたが私のコードで見ることができるように、collectionViewのセルを作成しました。物事はInstagramのようなフィードを作成したいということです。ボタンを押すと、UIcollectonViewにセルを追加したいのですか?それを行うことは可能ですか?はいの場合、どのようにですか?ボタンを押すとSwiftのUicollectionViewControllerにセルが追加されます

ps。イムは、ストーリーボードを使用していない

import UIKit 
import Firebase 
import MapKit 

class HomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

let cellId = "cellId" 

override func viewDidLoad() { 
    super.viewDidLoad() 

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Share", style: .Plain, target: self, action: #selector(handleLogout)) 

    navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .Plain, target: self, action: #selector(handleLogout)) 

    navigationItem.title = "Home" 

    collectionView?.alwaysBounceVertical = true 

    collectionView?.backgroundColor = UIColor(white: 0.95, alpha: 1) 
    collectionView?.registerClass(FeedCell.self, forCellWithReuseIdentifier: cellId) 


} 

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 1 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    return collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    return CGSizeMake(view.frame.width, 450) 
} 

func handleLogout() { 

    do { 
     try FIRAuth.auth()?.signOut() 
    } catch let logoutError { 
     print(logoutError) 
    } 

    let loginContoller = LoginController() 
    presentViewController(loginContoller, animated: true, completion: nil) 

} 

func addFeedCell() { 

} 



} 
class FeedCell: UICollectionViewCell, UICollectionViewDelegateFlowLayout { 

var wiindow: UIWindow? 
var mapView: MKMapView? 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    setupViews() 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

let nameLabel: UILabel = { 
    let label = UILabel() 
    label.text = "User Name" 
    label.font = UIFont.boldSystemFontOfSize(14) 
    label.translatesAutoresizingMaskIntoConstraints = false 
    return label 
}() 

let profileImageView: UIImageView = { 
    let imageView = UIImageView() 
    imageView.contentMode = .ScaleAspectFit 
    imageView.backgroundColor = UIColor.blueColor() 
    imageView.translatesAutoresizingMaskIntoConstraints = false 
    imageView.layer.cornerRadius = 22 
    imageView.layer.masksToBounds = true 
    return imageView 
}() 

let separatorView: UIView = { 
    let view = UIView() 
    view.backgroundColor = UIColor(red: 192/255, green: 192/255, blue: 192/255, alpha: 1) 
    view.translatesAutoresizingMaskIntoConstraints = false 
    return view 
}() 

func setupViews() { 

    addSubview(profileImageView) 
    addSubview(nameLabel) 
    addSubview(separatorView) 

    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[v0(44)]-10-[v1]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView, "v1": nameLabel])) 

    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[v0(44)]", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": profileImageView])) 

    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-10-[v0]-245-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel])) 

    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": separatorView])) 

    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[v0(1)]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": separatorView])) 

    self.wiindow = UIWindow(frame: UIScreen.mainScreen().bounds) 
    self.backgroundColor = UIColor(white: 0.95, alpha: 1) 

    self.mapView = MKMapView(frame: CGRectMake(0, 70, (self.wiindow?.frame.width)!, 355)) 
    self.addSubview(self.mapView!) 

    self.mapView!.zoomEnabled = false 
    self.mapView!.scrollEnabled = false 
    self.mapView!.userInteractionEnabled = false 






} 


} 

答えて

3
あなたはセルを挿入するために、あなたの UICollectionViewに( Array<>のような)データソースを添付しなければならない

。これは、collectionView:numberOfItemsInSection:デリゲートメソッドで固定数のセル(1)を返すためです。

新しいセルを挿入するように指定されたUICollectionViewの唯一の方法は、最初にデータソースの更新を必要とするinsertItemsAtIndexPaths:です。

これは非常に単純な例です:

var items = ["a", "b", "c"] 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return items.count 
} 

func didTapButton() { 
    // "logical" insert 
    let newChar = "d" 
    items.append(newChar) 

    // "graphical" insert 
    let newIndexPath = NSIndexPath(forItem: items.indexOf(newChar)!, inSection: 0) 
    collectionView.insertItemsAtIndexPaths([newIndexPath]) 
} 

//other delegate methods of UICollectionView not implemented in this example 

また、この質問を見て、答えを取る:How to insert Cell in UICollectionVIew Programmatically?

+0

ありがとうございました!今私はもう少し理解できる!:)私は今、良いビデオチュートリアルを見つけましたが、あなたが書いたのとほとんど同じことです。 – Stevic

関連する問題