[英]How to solve memory warning in Xcode
在我的應用程序中,我正在使用custom table view
,
因此,在一個單元格中,為server url
設置了很多圖像,
還可以使用一個滾動視圖來顯示圖像和視頻,並且還提供文本信息。 所以應用程序收到內存警告並且應用程序將崩潰,我試圖解決,但我沒有解決。有任何想法,請幫幫我。
下面是我的代碼,請檢查一下並幫助我解決這個問題
- (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);
}
謝謝
嘗試使用UICollectionView
而不是UIScrollView
。
在方法InnerScrollView:jsonValue:
,您將幾個UIButton
添加到ImgScroll
,在調用dequeueReusableCellWithIdentifier
時可以重用。
在initWithStyle:reuseIdentifier:
創建單元格時,創建所有子視圖。
或者,也許你可以嘗試刪除所有子視圖ImgScroll
你之前addSubview
它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.