UICollectionView: ObjC Apple Beispiel-code, Swift-Konvertierung (nicht finden konnte-Mitglied)

Ich versucht, die Umwandlung der sample code von Apple für die UICollectionView (https://developer.apple.com/library/prerelease/ios/samplecode/CircleLayout/Introduction/Intro.html) zu Swift.

Jedoch beim schreiben (View Controller, Zeile 82): self.collectionView.performBatchUpdates(self.collectionView.deleteItemsAtIndexPaths(NSArray(object: tappedCellPath)), completion: nil)

Bekomme ich die Fehlermeldung

Could not find member 'performBatchUpdates'

Ich daher gar nicht soweit Gebäude.

Hier ist mein vollständiger Code:

AppDelegate.swift

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //Override point for customization after application launch.
        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        //Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        //Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        //Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        //If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        //Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        //Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        //Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

ViewController.swift

import UIKit

let reuseIdentifier = "Cell"

class ViewController: UICollectionViewController {

    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        //Custom initialization
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        //Uncomment the following line to preserve selection between presentations
        //self.clearsSelectionOnViewWillAppear = false

        //Register cell classes
        cellCount = 20
        var tapRecognizer : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
        self.collectionView.addGestureRecognizer(tapRecognizer)
        self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView.reloadData()
        self.collectionView.backgroundColor = UIColor.scrollViewTexturedBackgroundColor()
        //Do any additional setup after loading the view.
    }

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

    /*
    //#pragma mark - Navigation

    //In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    {
    //Get the new view controller using [segue destinationViewController].
    //Pass the selected object to the new view controller.
    }
    */

    //#pragma mark UICollectionViewDataSource

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView?) -> Int {
        //#warning Incomplete method implementation -- Return the number of sections
        return 0
    }


    override func collectionView(collectionView: UICollectionView?, numberOfItemsInSection section: Int) -> Int {
        //#warning Incomplete method implementation -- Return the number of items in the section
        return cellCount!
    }

    override func collectionView(collectionView: UICollectionView?, cellForItemAtIndexPath indexPath: NSIndexPath?) -> UICollectionViewCell? {
        let cell = collectionView?.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell

        //Configure the cell

        return cell
    }

    func handleTapGesture(sender: UITapGestureRecognizer) {

        if (sender.state == .Ended) {
            println("tap")
            var initialPinchPoint : CGPoint = sender.locationInView(self.collectionView)
            var tappedCellPath : NSIndexPath = self.collectionView.indexPathForItemAtPoint(initialPinchPoint)

            if (tappedCellPath != nil) {
                cellCount = cellCount!-1
                self.collectionView.performBatchUpdates(self.collectionView.deleteItemsAtIndexPaths(NSArray(object: tappedCellPath)), completion: nil)
            } else {
                cellCount = cellCount!+1
                self.collectionView.performBatchUpdates(self.collectionView.insertItemsAtIndexPaths(NSArray(object: tappedCellPath)), completion: nil)
            }
        }
    }

    //pragma mark <UICollectionViewDelegate>

    /*
    //Uncomment this method to specify if the specified item should be highlighted during tracking
    func collectionView(collectionView: UICollectionView?, shouldHighlightItemAtIndexPath indexPath: NSIndexPath?) -> Bool {
    return true
    }
    */

    /*
    //Uncomment this method to specify if the specified item should be selected
    func collectionView(collectionView: UICollectionView?, shouldSelectItemAtIndexPath indexPath: NSIndexPath?) -> Bool {
    return true
    }
    */

    /*
    //Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
    func collectionView(collectionView: UICollectionView?, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath?) -> Bool {
    return false
    }

    func collectionView(collectionView: UICollectionView?, canPerformAction action: String?, forItemAtIndexPath indexPath: NSIndexPath?, withSender sender: AnyObject) -> Bool {
    return false
    }

    func collectionView(collectionView: UICollectionView?, performAction action: String?, forItemAtIndexPath indexPath: NSIndexPath?, withSender sender: AnyObject) {

    }
    */

}

Zelle.swift

import UIKit

class Cell: UICollectionViewCell {

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

        self.contentView.layer.cornerRadius = 35
        self.contentView.layer.borderWidth = 1
        self.contentView.layer.borderColor = UIColor.whiteColor().CGColor
        self.contentView.backgroundColor = UIColor.clearColor()
    }

}

CircleLayout.swift

import UIKit
import QuartzCore

var cellCount : NSInteger?
var center : CGPoint?
var radius : CGFloat?

var deleteIndexPaths : NSMutableArray?
var insertIndexPaths : NSMutableArray?

class CircleLayout: UICollectionViewLayout {
    var centerX : CGFloat?
    var centerY : CGFloat?


    override func prepareLayout() {
        super.prepareLayout()

        var size : CGSize = self.collectionView.frame.size
        cellCount = self.collectionView.numberOfItemsInSection(0)
        center = CGPointMake(size.width/2.0, size.height/2.0)
        centerX = center!.x
        centerY = center!.y
        radius = 300
    }

    override func collectionViewContentSize() -> CGSize {
        return self.collectionView.frame.size
    }

    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath!) -> UICollectionViewLayoutAttributes! {
        var attributes : UICollectionViewLayoutAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        attributes.size = CGSizeMake(70, 70)
        attributes.center = CGPointMake(0,0)
        return attributes
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> AnyObject[]! {
        var attributes : NSMutableArray = NSMutableArray.array()

        for (var i = 0; i<cellCount!; i++) {
            var indexPath : NSIndexPath = NSIndexPath(forItem: i, inSection: 0)
            attributes.addObject(self.layoutAttributesForItemAtIndexPath(indexPath))
        }
        return attributes
    }

    override func prepareForCollectionViewUpdates(updateItems: AnyObject[]!) {

        super.prepareForCollectionViewUpdates(updateItems)

        deleteIndexPaths = NSMutableArray.array()
        insertIndexPaths = NSMutableArray.array()

        for update : AnyObject in updateItems{
            if (update.updateAction == UICollectionUpdateAction.Delete) {
                deleteIndexPaths!.addObject(update.indexPathBeforeUpdate)
            } else if (update.updateAction == UICollectionUpdateAction.Insert) {
                insertIndexPaths!.addObject(update.indexPathAfterUpdate)
            }
        }
    }

    override func finalizeCollectionViewUpdates() {
        deleteIndexPaths = nil
        insertIndexPaths = nil
    }

    override func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath!) -> UICollectionViewLayoutAttributes! {

        var attributes : UICollectionViewLayoutAttributes = super.initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath)

        if (insertIndexPaths!.containsObject(itemIndexPath)) {
            if(attributes == false) {
                attributes = self.layoutAttributesForItemAtIndexPath(itemIndexPath)
                attributes.alpha = 0.0
                attributes.center = CGPointMake(center!.x, center!.y)
            }
        }
        return attributes
    }

    override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath!) -> UICollectionViewLayoutAttributes! {
        var attributes : UICollectionViewLayoutAttributes = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath)

        if (deleteIndexPaths!.containsObject(itemIndexPath)) {
            if(attributes == false) {
                attributes = self.layoutAttributesForItemAtIndexPath(itemIndexPath)
                attributes.alpha = 0.0
                attributes.center = CGPointMake(center!.x, center!.y)
                attributes.transform3D = CATransform3DMakeScale(0.1,0.1,1.0)
            }
        }
        return attributes

    }

}
  • Bitte entfernen Sie alle irrelevanten code. Ernst - niemand wird Lesen Sie alles, was.
  • 1. Haben Sie versucht, die Aufteilung der Zeile, die fehlschlägt, in ein paar Zeilen (mit temporären Variablen)? Weil der Swift-compiler-Fehler, manchmal wird es magisch hilft. 2. Haben Sie versucht, die Reinigung der Ziel-und Umbau? Das hilft auch manchmal.
  • Ich weiß nicht, in welchem Teil des Codes das problem verwurzelt ist. Wenn ich nur nach dem code, dass ich denke, relevant ist, könnte ich den Teil verpasst, dass die das problem verursacht.
  • Ich habe versucht zu tun, aber das hat mich nicht sehr weit. Könnten Sie mir ein Beispiel, was ich versuchen sollte?
InformationsquelleAutor chl | 2014-07-08
Schreibe einen Kommentar