UICollectionView的内存使用率非常高

时间:2016-08-26 20:47:08

标签: ios swift xcode uicollectionview uicollectionviewcell

我遇到内存使用问题。每当我滚动或重新加载数据时,内存使用量会持续上升而且永远不会下降。我有一种感觉,由于某种原因,细胞没有释放任何显示的项目。

非常感谢任何帮助。我很确定我做错了什么,但我似乎无法找到什么。

提前谢谢

以下是创建我的UICollectionView的代码:

import Foundation
import UIKit
import CoreData
import Crashlytics

public class podCollectionView : UIViewController,UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIPopoverPresentationControllerDelegate  {
private var collectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())   // Initialization
private var inmates : NSArray = [];
private var context : NSManagedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext;
private var configuration = Configuration.sharedInstance;
private var titleView = UILabel();
private var database : FMDatabase = (UIApplication.sharedApplication().delegate as! AppDelegate).database!;


public override func viewDidLoad() {
    getInmates();
    super.viewDidLoad();
    self.collectionView = podMainView(x: "1", y: "2");
    self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "CellCell") // UICollectionViewCell
    self.collectionView.backgroundColor = UIColor.clearColor();
    self.collectionView.layer.cornerRadius = 5;
    self.collectionView.layer.masksToBounds = true;
    self.collectionView.delegate = self     // delegate  :  UICollectionViewDelegate
    self.collectionView.dataSource = self   // datasource  : UICollectionViewDataSource

    self.view.addSubview(self.collectionView);

    self.view = self.collectionView;
    self.navigationItem.setHidesBackButton(true, animated:false);

    var inmateRefresh: NSTimer!
    gameTimer = NSTimer.scheduledTimerWithTimeInterval(120, target: self, selector: #selector(refreshDAta), userInfo: nil, repeats: true);



}

public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return self.inmates.count;
}

public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let retVal : UICollectionViewCell = self.collectionView.dequeueReusableCellWithReuseIdentifier("CellCell", forIndexPath: indexPath);
    retVal.prepareForReuse();
    retVal.addSubview(InmateSummary(inmate: self.inmates.objectAtIndex(indexPath.item) as! NSArray,parent: self));
    return retVal;
}


private func getInmates(){
    var totalInmates = 0;
    var inmatesInArea = 0;
    var inmatesOutOfArea = 0;
    self.inmates = [];
    do{
        let tmpInmates : NSMutableArray = [];
        let rs = try self.database.executeQuery("SELECT * FROM inmates WHERE podName='"+self.configuration.currentArea+"' ORDER BY suicideBlue DESC", values: nil)
        while rs.next() {
             let rowData : NSMutableArray = []
            let colCount = Int(rs.columnCount()) as Int;
            for i in 0 ..< colCount {
                rowData.addObject(rs.objectForColumnName(rs.columnNameForIndex(Int32(i))));
            }


            if rowData[7] as! String == "" {
                inmatesInArea = inmatesInArea + 1;
            }else{
                inmatesOutOfArea = inmatesOutOfArea + 1;
            }
            totalInmates = totalInmates + 1;
            tmpInmates.addObject(rowData);
        }

        self.inmates = tmpInmates;


    } catch {
        CLSLogv("Error while loading inmates in main screen \(error)%@", getVaList(["three"]));
    }

    let barText = String(inmatesInArea) + "/" + String(inmatesOutOfArea) + " (" + String(totalInmates) + ")";

    let buttonBack: UIButton = UIButton(type: UIButtonType.Custom) as UIButton
    buttonBack.frame = CGRectMake(0, 0, 100, 40)
    buttonBack.setTitle(barText, forState: .Normal);

    let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: buttonBack)

    self.navigationItem.setLeftBarButtonItem(leftBarButtonItem, animated: false)
    //self.database.close();
}

public func changePod(pod:String){
    self.configuration.currentArea = pod;
    self.titleView.text = pod + " ▼";
    self.getInmates();
    self.collectionView.reloadData();
}

public func refreshDAta(){
    self.database.close();
    self.database.open();

        let inmateFetcer = InmateFetcher();
        inmateFetcer.getUpdates();


        self.getInmates();
        //self.inmates = [];
        self.collectionView.reloadData();

        NSLog("DATA REFRESHED");


}

override public func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}
}

以下是单元格中视图的代码:

import Foundation
import UIKit


public class InmateSummary : UIView, UIPopoverPresentationControllerDelegate{
public var inmate:NSArray;
private var screenSize : CGRect = UIScreen.mainScreen().bounds;
private var configuration = Configuration.sharedInstance;
private var parent : podCollectionView;

init(inmate:NSArray, parent:podCollectionView){
    let cellHeight = screenSize.height * 0.295;
    let cellWidth = screenSize.width * 0.295;
    self.inmate = inmate;
    self.parent = parent;
    super.init(frame: CGRect(x: 0, y: 0, width: cellWidth, height: cellHeight));

    let aSelector : Selector = #selector(InmateSummary.inmateClicked(_:));
    let tapGesture = UITapGestureRecognizer(target: self, action: aSelector);
    tapGesture.numberOfTapsRequired = 1;
    self.addGestureRecognizer(tapGesture);

    self.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1);

    let topView = UIView(frame: CGRect(x: 0, y: 0, width: cellWidth, height: cellHeight*0.10));
    topView.backgroundColor = UIColor.grayColor();

    self.addSubview(topView);

    let cellLab = UILabel(frame: CGRect(x: 0, y: 0, width: cellWidth, height: cellHeight*0.10));
    cellLab.text = self.inmate[3] as? String;
    cellLab.textColor = UIColor.whiteColor();
    cellLab.font = UIFont.init(name:"Arial-BoldMT", size: 30);
    cellLab.textAlignment = NSTextAlignment.Center;

    self.addSubview(cellLab);

    let sexLab = UILabel(frame: CGRect(x: cellWidth*0.75, y: 0, width: cellWidth*0.25, height: cellHeight*0.10));
    sexLab.text = self.inmate[1] as? String
    sexLab.textColor = UIColor.whiteColor();
    sexLab.textAlignment = NSTextAlignment.Center;

    self.addSubview(sexLab);

    let greyView = UIView(frame: CGRect(x: 0, y: cellHeight*0.10, width: cellWidth, height: cellHeight*0.70));
    greyView.backgroundColor = UIColor.grayColor();

    self.addSubview(greyView);

    if self.inmate[7] as? String == "" {
        greyView.backgroundColor = UIColor.blackColor();
    }



    let eventLab = UILabel(frame: CGRect(x: cellWidth*0.75, y: 0, width: cellWidth*0.25, height: cellHeight*0.10));
    eventLab.text = self.inmate[7] as? String;
    eventLab.textColor = UIColor.whiteColor();
    eventLab.textAlignment = NSTextAlignment.Center;
    eventLab.adjustsFontSizeToFitWidth = true;

    greyView.addSubview(eventLab);


    let classLab = UILabel(frame: CGRect(x: 0, y: 0, width: cellWidth*0.25, height: cellHeight*0.10));
    classLab.text = self.inmate[4] as? String;
    classLab.textAlignment = NSTextAlignment.Center;
    classLab.font = UIFont.init(name:"Arial-BoldMT", size: 20);
    classLab.adjustsFontSizeToFitWidth = true;

    if(classLab.text == "RISK"){
        classLab.textColor = UIColor.yellowColor();
    }else if(classLab.text == "7CLS"){
        classLab.textColor = UIColor(red: 1.0, green:0.5, blue:0.0, alpha:1);
    }else if(classLab.text == "8MAX"){
        classLab.textColor = UIColor(red:1.0, green:0.0, blue:0.0, alpha:0.7);
    }else{
        classLab.textColor = UIColor.whiteColor();
    }

    greyView.addSubview(classLab);
    var mugshot : UIImage = UIImage();
    if (self.inmate[12] as? NSData) != nil {
        mugshot = UIImage(data: (self.inmate[12] as? NSData)!)!;
    }else{
        NSLog("Inmate without mugshot: %@",self.inmate[2] as! String);
    }
    let mugshotView = UIImageView(frame: CGRect(x: cellWidth*0.16, y: cellHeight*0.09, width: cellWidth*0.66, height: cellHeight*0.5));
    mugshotView.image = mugshot;

    greyView.addSubview(mugshotView);

    let bookingNumber = UILabel(frame: CGRect(x: cellWidth*0.25, y: cellHeight*0.545, width: cellWidth*0.50, height: cellHeight*0.15));
    bookingNumber.text = self.inmate[2] as? String;
    bookingNumber.textColor = UIColor.whiteColor();
    bookingNumber.textAlignment = NSTextAlignment.Center;
    bookingNumber.font = UIFont.init(name:"Arial-BoldMT", size: 20);
    bookingNumber.adjustsFontSizeToFitWidth = true;

    greyView.addSubview(bookingNumber);

    let nameView = UIView(frame: CGRect(x: 0, y: cellHeight*0.75, width: cellWidth, height: cellHeight*0.25));


    self.addSubview(nameView);

    let maxRed = UIView(frame: CGRect(x: 0, y: 0, width: cellWidth*0.33, height: cellHeight*0.115));
    maxRed.backgroundColor = UIColor.blackColor();
    if (self.inmate[15] as! NSNumber == 1) {
        maxRed.backgroundColor = UIColor.redColor();
    }
    nameView.addSubview(maxRed);

    let suBlue = UIView(frame: CGRect(x: cellWidth*0.33, y: 0, width: cellWidth*0.33, height: cellHeight*0.115));
    suBlue.backgroundColor = UIColor.blackColor();
    if (self.inmate[18] as! NSNumber  == 1) {
        let aSelector : Selector = #selector(InmateSummary.suicideClicked(_:));
        let tapGesture = UITapGestureRecognizer(target: self, action: aSelector);
        tapGesture.numberOfTapsRequired = 1;
        nameView.addGestureRecognizer(tapGesture);
        suBlue.backgroundColor = UIColor.blueColor();
    }
    nameView.addSubview(suBlue);
    let segGreen = UIView(frame: CGRect(x: cellWidth*0.66, y: 0, width: cellWidth*0.33, height: cellHeight*0.115));
    segGreen.backgroundColor = UIColor.blackColor();
    if (self.inmate[5] as! NSNumber == 1) {
        segGreen.backgroundColor = UIColor.greenColor();
    }
    nameView.addSubview(segGreen);



    let nameLab = UILabel(frame: CGRect(x: cellWidth*0.025, y: cellHeight*0.12, width: cellWidth*0.95, height: cellHeight*0.05));
    nameLab.text = self.inmate[0] as? String;
    nameLab.textAlignment = NSTextAlignment.Center;
    nameLab.textColor = UIColor.whiteColor();
    nameLab.font = UIFont.init(name:"Arial-BoldMT", size: 20);
    nameLab.adjustsFontSizeToFitWidth = true;
    nameView.addSubview(nameLab);

    if(self.inmate[18] as! NSNumber == 1){
        //NSLog("Suicide: %@",self.inmate);
        let checkLab = UILabel(frame: CGRect(x: 0, y: cellHeight*0.17, width: cellWidth, height: cellHeight*0.07));

        let formatter = NSDateFormatter()
        formatter.dateFormat = "MM/dd HH:mm"
        let tmpDate = NSDate(timeIntervalSince1970: (self.inmate[19] as! NSString).doubleValue)
        let dateString = formatter.stringFromDate(tmpDate);

        checkLab.text = (self.inmate[22] as? String)! + " - " + dateString;
        checkLab.textAlignment = NSTextAlignment.Center;
        checkLab.textColor = UIColor.whiteColor();
        checkLab.font = UIFont.init(name:"Arial", size: 18);
        checkLab.adjustsFontSizeToFitWidth = true;
        nameView.addSubview(checkLab);
    }

    if self.inmate[1] as? String == "F" {
        nameView.backgroundColor = UIColor.purpleColor();
    }

    self.layer.cornerRadius = 5;
    self.layer.borderColor = UIColor.grayColor().CGColor;
    self.layer.borderWidth = 1;
    self.layer.masksToBounds = true;

}
func suicideClicked(sender:UITapGestureRecognizer!){
    let podsChooser = ActivityChooser(parentView:self);

    podsChooser.modalPresentationStyle = .Popover;

    let popoverMenuViewController = podsChooser.popoverPresentationController

    popoverMenuViewController!.permittedArrowDirections = .Any
    popoverMenuViewController!.delegate = self
    popoverMenuViewController!.sourceView = sender.view;
    self.parent.presentViewController(
        podsChooser,
        animated: true,
        completion: nil)
}

func inmateClicked (sender:InmateSummary){
    NSLog("Inmate Clicked");

    let vc = SingleInmateDisplay(inmate: inmate);
    self.parent.navigationController!.pushViewController(vc,animated:false);
}

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

}

0 个答案:

没有答案