如何解决Xcode中的内存警告问题

时间:2015-12-16 07:19:08

标签: ios objective-c uitableview memory-management

在我的应用程序中,我正在使用custom table view

所以在一个单元格中有很多图像设置为server url

还使用一个scrollview进行图像和视频显示,文本信息也在那里。所以应用程序获取内存警告和应用程序崩溃,我试图解决,但我没有解决。你有任何想法,所以帮助我。

下面是我的代码,请检查并帮助我解决此问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView.tag == 0) {

            static NSString *CellIdentifier = @"Cell";

            TableViewCell *TBLcell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

            TBLcell.delegate = self;
            TBLcell.index = indexPath.row;

            if (TBLcell.frame.size.height == 281) {
                TBLcell.DescriptionViewHeightConstraint.constant = 40.0;
            }else{
                TBLcell.DescriptionViewHeightConstraint.constant = 80.0;
            }

            [TBLcell setSelectionStyle:UITableViewCellSelectionStyleNone];


            NSString *strURL = [[arrBlogList objectAtIndex:indexPath.row] valueForKey:@"image"];
            NSString *strProfileImageURL = [URLofImageUpload stringByAppendingString:strURL];
            NSURL * imageURL = [NSURL URLWithString:strProfileImageURL];
            [[JMImageCache sharedCache] imageForURL:imageURL completionBlock:^(UIImage *image) {
                TBLcell.ProfileImg.image= image;
            } failureBlock:^(NSURLRequest *request, NSURLResponse *response, NSError *error) {
                TBLcell.ProfileImg.image= [UIImage imageNamed:@"user icon"];
            }];

            NSString *strURLCat = [[arrBlogList objectAtIndex:indexPath.row] valueForKey:@"category_image"];
            NSString *strCategoryImageURL = [URLofImageUpload stringByAppendingString:strURLCat];
            NSURL * imageCatURL = [NSURL URLWithString:strCategoryImageURL];
            [[JMImageCache sharedCache] imageForURL:imageCatURL completionBlock:^(UIImage *image) {
                TBLcell.imgProduct.image= image;
            } failureBlock:^(NSURLRequest *request, NSURLResponse *response, NSError *error) {
                TBLcell.imgProduct.image= [UIImage imageNamed:@"user icon"];
            }];

            TBLcell.imgProduct.contentMode = UIViewContentModeScaleAspectFit;

            NSMutableArray *strmedai = [[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"media"];

            NSLog(@"Media Data:\n%@", strmedai);

            TBLcell.ImgScroll.tag = indexPath.row;

            if (strmedai.count != 0)
            {
                TBLcell.ImgScroll.hidden = false;
                [self InnerScrollView:TBLcell jsonValue:strmedai];
            }
            else
            {
                NSLog(@"JSON value: %@",strmedai);
                TBLcell.ImgScroll.hidden = true;
            }

            CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
            CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
            CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
            TBLcell.contentView.backgroundColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
            TBLcell.lblStatus.textColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness+20 alpha:1];

            TBLcell.lblstatusMessage.backgroundColor = [UIColor colorWithHue:hue saturation:saturation+30 brightness:brightness alpha:1];

            TBLcell.btnleft.tag = TBLcell.btnRight.tag = TBLcell.ImgScroll.tag = TBLcell.btnShare.tag = indexPath.row ;

            TBLcell.lblName.text = [[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"title"];
            TBLcell.lbl_CreateBlogUser.text = [[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"fullname"];
            NSString *strprogress = [[[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"blog_progress"] stringByAppendingString:@"%"];

            TBLcell.lblProgressCount.text = strprogress;
            TBLcell.lblProgressCount.textAlignment = NSTextAlignmentCenter;



            TBLcell.lblLikeCount.text = [NSString stringWithFormat:@"%@",[[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"like_count"]];

            TBLcell.lblFavCount.text = [NSString stringWithFormat:@"%@",[[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"favorite"]];

            TBLcell.lblViewCount.text = [NSString stringWithFormat:@"%@",[[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"views"]];

            TBLcell.lblCommentCount.text = [NSString stringWithFormat:@"%@",[[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"comment_count"]];


             NSString *blogStatus = [[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"blog_status"];

            if ([blogStatus intValue] == 1)
                TBLcell.lblStatus.text = @"Done";

            else if ([blogStatus intValue] == 2)
                TBLcell.lblStatus.text = @"In Progress";

            else if ([blogStatus intValue] == 3)
                TBLcell.lblStatus.text = @"Need Support";

            TBLcell.textVBlogDetail.text = [[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"description"];

            TBLcell.lblLocation.text = [[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"location"];

            NSString *strDate = [[arrBlogList objectAtIndex:indexPath.row]valueForKey:@"blog_date"];

            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
            [dateFormatter setDateFormat:@"yyyy-MM-dd"]; //// here set format of date which is in your output date (means above str with format)

            NSDate *date = [dateFormatter dateFromString: strDate]; // here you can fetch date from string with define format

            dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"dd MMM yyyy"];// here set format which you want...

            NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
          //  NSLog(@"Converted String : %@",convertedString);
            TBLcell.lblDate.text =convertedString;
            TBLcell.lblstatusMessage.layer.cornerRadius = 5;
            TBLcell.lblstatusMessage.text = [NSString stringWithFormat:@"   Status: %@",[[arrBlogList objectAtIndex:indexPath.row] valueForKey:@"blog_status"]];

            TBLcell.lblstatusMessage.layer.masksToBounds = YES;


            return TBLcell;
        }
        return nil;
    }
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (tableView.tag == 0)
        {
            objAppDelegate.arrBlogs = [[NSMutableArray alloc]init];
            [objAppDelegate.arrBlogs addObject:[arrBlogList objectAtIndex:indexPath.row]];

            SelectedBlogVC *objselectedBlog = [[self storyboard]instantiateViewControllerWithIdentifier:@"SelectedBlogVC"];
            [[self navigationController]pushViewController:objselectedBlog animated:YES];
        }

    }

    -(void)InnerScrollView:(TableViewCell *)_cell jsonValue:(NSArray  *)jsonValue
    {
        _cell.ImgScroll.pagingEnabled = YES;
        [_cell.ImgScroll setAlwaysBounceVertical:NO];
        //setup internal views

        UIButton *btnimagetap;

        for (int i = 0; i < [jsonValue count]; i++)
        {
            CGFloat xOrigin = i * 70;

            btnimagetap = [[UIButton alloc] initWithFrame:CGRectMake(xOrigin, 0, 70, 70)];
            btnimagetap.tag = i;
            btnimagetap.backgroundColor = [UIColor clearColor];
                    [_cell.ImgScroll addSubview:btnimagetap];
            [btnimagetap addTarget:self
                            action:@selector(ImagTap_method:)
                  forControlEvents:UIControlEventTouchUpInside];

            NSString *strType = [[jsonValue valueForKey:@"type"]objectAtIndex:i];
            if ([strType isEqualToString:@"Video"])
            {
                [btnimagetap setImage:[UIImage imageNamed:@"screen6-home-list-iew_55"] forState:UIControlStateNormal];
            }

            else
            {

                NSString *strCategoryImageURL = [URLofImageUpload stringByAppendingString:[[jsonValue valueForKey:@"name"]objectAtIndex:i]];
                NSURL * imageCatURL = [NSURL URLWithString:strCategoryImageURL];
                [[JMImageCache sharedCache] imageForURL:imageCatURL completionBlock:^(UIImage *image) {
                    [btnimagetap setImage:image forState:UIControlStateNormal];
                } failureBlock:^(NSURLRequest *request, NSURLResponse *response, NSError *error) {
                    [btnimagetap setImage:[UIImage imageNamed:@"user icon"] forState:UIControlStateNormal];
                }];            
            }
        }
        NSInteger numberOfViews = [jsonValue count]/4;
        if (numberOfViews == 0) numberOfViews = 1;
        //set the scroll view content size
        _cell.ImgScroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, _cell.ImgScroll.frame.size.height);
    }

谢谢

1 个答案:

答案 0 :(得分:0)

尝试使用UICollectionView代替UIScrollView

在方法InnerScrollView:jsonValue:中,您要向UIButton添加多个ImgScroll,这可能会在调用dequeueReusableCellWithIdentifier时重复使用。

initWithStyle:reuseIdentifier:创建单元格时创建所有子视图。

或者您可以尝试在ImgScroll之前删除addSubview中的所有子视图。