CollectionView内存VM:分配和放弃CoreAnimation内存

时间:2014-08-01 13:57:11

标签: ios uicollectionview uicollectionviewcell

我有一些在同一个视图控制器中的collectionViews和一个表视图。相当奇怪的问题是,当我向上滚动时,我总是会增加内存。 仪器显示了很多VM:CoreAnimation对象的分配但我无法跟踪它们(它们在collectionView本身内)。 准备重用是在单元格中调用,我检查了这个。

以下是代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.receivedChannels = NO;
    self.ItemsDict = [NSMutableDictionary new];
    self.crtBatchSet = [NSMutableIndexSet new];


    if ([[PSDeviceInfo sharedInstance] is_iPad]) {
        self.epgWidth = EPG_WIDTH_IPAD;
    } else {
        self.epgWidth = EPG_WIDTH_IPHONE;
    }
    crtBatchSetSize = 0;
    self.currentSelecteIndexOfDateCell = 0;
    //size
    self.widthDictionary = [NSMutableDictionary new];
    self.centerXDictionary = [NSMutableDictionary new];

    self.layout = [[MultipleLineLayout alloc] initWithWidthDictionary:self.widthDictionary andCenterXDictionary:self.centerXDictionary];
    self.ItemsCollectionVIew.collectionViewLayout = self.layout;
    self.ItemsCollectionVIew.showsHorizontalScrollIndicator = NO;
    self.ItemsCollectionVIew.showsVerticalScrollIndicator = NO;
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    [self.ItemsCollectionVIew registerClass:[ItemColectionCell class] forCellWithReuseIdentifier:@"ItemColectionCellID"];
    [self.hoursCollectionView registerClass:[HoursCollectionViewCell class] forCellWithReuseIdentifier:@"HoursColectionCellID"];
    [self.datePickerCollectionView registerClass:[DatePickerCollectionViewCell class] forCellWithReuseIdentifier:@"DatePickerColectionCellID"];


 }

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.ItemsCollectionVIew){
        self.channelsTableView.contentOffset = CGPointMake(0, self.ItemsCollectionVIew.contentOffset.y);
        self.hoursCollectionView.contentOffset = CGPointMake(self.ItemsCollectionVIew.contentOffset.x, 0);
    }
    if (scrollView == self.channelsTableView) {
        self.ItemsCollectionVIew.contentOffset = CGPointMake(self.ItemsCollectionVIew.contentOffset.x,self.channelsTableView.contentOffset.y);
    }
    if (scrollView == self.hoursCollectionView) {
        self.ItemsCollectionVIew.contentOffset = CGPointMake(self.hoursCollectionView.contentOffset.x,self.ItemsCollectionVIew.contentOffset.y);
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *channelCellID = @"ChannelTableCellID";
    ChannelTableCell *cell = [tableView dequeueReusableCellWithIdentifier:channelCellID forIndexPath:indexPath];
    cell.myIndexInTable = indexPath.row;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    [cell getData];
    return cell;
}


#pragma mark - UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.ItemsCollectionVIew reloadData];
}

#pragma mark - UICollectionViewDataSource Methods

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (collectionView == self.datePickerCollectionView || collectionView == self.hoursCollectionView) {
        return 1;
    }else{
        if (self.receivedChannels) {
            return totalNoChannels;
        } else {
            return 1;
        }
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == self.datePickerCollectionView) {
        return DELTA_DAYS + 1;
    }else if (collectionView == self.hoursCollectionView) {
        return 48; //24 hours  *  2
    }else{
//        if (collectionView == self.ItemsCollectionVIew){
        NSArray *sectionItems = self.ItemsDict[@(section)];
        if (sectionItems) {
           // return sectionItems.count;
            return 100;
        } else {
            return 100;
        }
    }

    return 1;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    if (collectionView == self.datePickerCollectionView) {
        static NSString *ItemCellID = @"DatePickerColectionCellID";
        DatePickerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
        [self setDayAndDateforCell:cell at:indexPath];
        [cell setBackgroundColor:[self getColorForCell:self.currentSelecteIndexOfDateCell == indexPath.row ? YES:NO]];
        return cell;
    }else if (collectionView == self.hoursCollectionView){
        static NSString *ItemCellID = @"HoursColectionCellID";
        HoursCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];
        cell.hourLabel.text = [self getTimeLineCellValuerFor:indexPath];
        return cell;
    } else{
//    if (collectionView == self.datePickerCollectionView) {
        static NSString *ItemCellID = @"ItemColectionCellID";
        ItemColectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemCellID forIndexPath:indexPath];

        NSArray *Items = self.ItemsDict[@(indexPath.section)];
        if (indexPath.row >= Items.count) {
            return cell;
        }
        MvpItem *prog = Items[indexPath.row];

        [cell updateInfo:prog];

        [cell setBackgroundColor:[self getColorForCell:NO]];

        return cell;
    }
}

#pragma mark - UICollectionViewDelegate Methods
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.datePickerCollectionView) {
        self.currentSelecteIndexOfDateCell = indexPath.row;
    }
    if (collectionView == self.ItemsCollectionVIew) {
        [self pushDetailsatIndexPath:indexPath];
    }
}

tableView单元格的代码是:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self.layer setBorderColor:[UIColor blackColor].CGColor];
        [self.layer setBorderWidth:1.0f];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)prepareForReuse{
    self.thumbnailImage.image = nil;
    self.channel = nil;
    [self clearDelegate];
    [super prepareForReuse];
}

- (void)dealloc
{
    [self clearDelegate];
}

- (void)clearDelegate
{
    NSString *url = self.channel.media_content.thumbPoster.url;
  //  [[ImageManager sharedInstance] removeDelegate:self forImgUrl:url];
    [[TVManager sharedInstance] removeDelegate:self];
}

- (void)getData
{
    [[TVManager sharedInstance] getChannelAndItemssForIndex:self.myIndexInTable forDateDelta:0 forDelegate:self];
}

- (void)didReceiveTotalNoChn:(NSInteger)totalChnNo{
    return;
}

- (void)didReceiveChannel:(MvpChannel *)channel withItemss:(NSArray *)Itemss forDateDelta:(NSInteger)date withIndex:(NSUInteger)index{
    if (index != self.myIndexInTable) {
        return;
    }
    if ([self.channel isEqual:channel]) {
        return;
    }
    self.channel = channel;
    NSString *imgUrl = self.channel.media_content.thumbPoster.url;
//    UIImage *img = [[ImageManager sharedInstance] getImageForUrl:imgUrl forIndex:self.myIndexInTable withDelegate:self];
//    if (img) {
//        self.thumbnailImage.image = img;
//    }
}

#pragma mark - Image Delegate

- (void)didReceiveImage:(UIImage *)image forIndex:(NSInteger)index
{
    if (index != self.myIndexInTable) {
        return;
    }
    if (image) {
        self.thumbnailImage.contentMode = UIViewContentModeScaleAspectFit;
        self.thumbnailImage.image = image;
        [self setNeedsLayout];
    }
}

在collectionViewCell中:

@interface ItemColectionCell ()

@property (retain, nonatomic) UILabel *titleLabel;
@property (retain, nonatomic) UILabel *genreLabel;
@property (retain, nonatomic) UILabel *intervalLabel;

@end

@implementation ItemColectionCell //collection

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self.layer setBorderColor:[UIColor blackColor].CGColor];
        [self.layer setBorderWidth:1.0f];

        self.titleLabel = [[UILabel alloc] init];
        self.genreLabel = [[UILabel alloc] init];
        self.intervalLabel = [[UILabel alloc] init];

        [self.titleLabel setBackgroundColor:[UIColor clearColor]];
        [self.genreLabel setBackgroundColor:[UIColor clearColor]];
        [self.intervalLabel setBackgroundColor:[UIColor clearColor]];

        [self.titleLabel setTextColor:[UIColor whiteColor]];
        [self.genreLabel setTextColor:[UIColor whiteColor]];
        [self.intervalLabel setTextColor:[UIColor whiteColor]];

        [self.titleLabel setFont:[UIFont fontWithName:@“cf-Bold" size:15]];;
        [self.genreLabel setFont:[UIFont fontWithName:@“cf-Regular" size:11]];;
        [self.intervalLabel setFont:[UIFont fontWithName:@“cf-Regular" size:11]];;

    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)prepareForReuse{
    self.titleLabel.text = nil;
    self.genreLabel.text = nil;
    self.intervalLabel.text = nil;
    [self.titleLabel removeFromSuperview];
    [self.genreLabel removeFromSuperview];
    [self.intervalLabel removeFromSuperview];
    [super prepareForReuse];
}

- (void)updateInfo:(MvpItem*)item{
    if (!item) {
        return;
    }

    NSDateFormatter *startTimeFormat = [[NSDateFormatter alloc] init];
    [startTimeFormat setDateFormat:@"hh:mm"];

    NSDateFormatter *endTimeFormat = [[NSDateFormatter alloc] init];
    [endTimeFormat setDateFormat:@"hh:mm a"];

    NSString *startTime = [startTimeFormat stringFromDate:item.startTime];
    NSString *endTime = [endTimeFormat stringFromDate:item.endTime];

    self.titleLabel.frame = CGRectMake(10, 7, self.frame.size.width - 10, 15);
    self.genreLabel.frame = CGRectMake(10, 20, self.frame.size.width - 10, 15);
    self.intervalLabel.frame = CGRectMake(10, 32, self.frame.size.width - 10, 15);

    self.titleLabel.text = item.title;
    self.genreLabel.text = @“tewst”;
    self.intervalLabel.text = [NSString stringWithFormat:@"%@ - %@", startTime, endTime];

    [self addSubview:self.titleLabel];
    [self addSubview:self.genreLabel];
    [self addSubview:self.intervalLabel];
}

我可以做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:2)

您是否尝试删除在结束显示时显示该单元格所触发的单元格或繁重任务?

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
相关问题