Tableview loading more than 50 images Received memory warning - ios

This is my cell data
for (int s=0; s<[arrayForPhotosData1 count]; s++) {
PhotoCell *cell=[[PhotoCell alloc] init];
cell.imgPhoto.layer.shadowColor = [UIColor lightGrayColor].CGColor;
cell.imgPhoto.layer.shadowOffset = CGSizeMake(0, 2);
cell.imgPhoto.layer.shadowOpacity = 1;
cell.imgPhoto.layer.shadowRadius = 1.0;
cell.imgPhoto.clipsToBounds = NO;
if([[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"resolution_280_280"]!=NULL)
{
[cell.imgPhoto loadImage:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"resolution_280_280"]];
}
[cell.ImgProfile loadImage:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"profile_picture"]];
[cell.lblUserName setText:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"caption"]];
[cell.lblForTime setText:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"created_time"]];
[cell.lblFullName setText:[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"Name" ]];
[cell.btnForLike addTarget:self
action:#selector(addlikes:)
forControlEvents:UIControlEventTouchUpInside];
[cell.btnForLike setImage:[UIImage imageNamed:#"like.png"] forState:UIControlStateNormal];
[cell.btnForFavourite addTarget:self
action:#selector(addFavourite:)
forControlEvents:UIControlEventTouchUpInside];
cell.btnForFavourite.tag=[[[arrayForPhotosData1 objectAtIndex:s]valueForKey:#"mediaid"] intValue];
cell.btnForComment.tag=[[[arrayForPhotosData1 objectAtIndex:s]valueForKey:#"mediaid"] intValue];
[cell.btnForComment addTarget:self action:#selector(addcomen:)forControlEvents:UIControlEventTouchUpInside];
cell.imgPhoto.tag=[[[arrayForPhotosData1 objectAtIndex:s]valueForKey:#"mediaid"] intValue];
[cell.imgPhoto addTarget:self action:#selector(addcoments:)forControlEvents:UIControlEventTouchUpInside];
cell.btnForLike.tag=s;
[cell.btnForLikeText setTitle:[NSString stringWithFormat:#"%# Me gusta",[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"Totallikes"]] forState:UIControlStateNormal];
[cell.btnForCommentText setTitle:[NSString stringWithFormat:#"%# Commentarios",[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"TotalComment"]] forState:UIControlStateNormal];
[cell.btnForLikeText addTarget:self
action:#selector(addlikes:)
forControlEvents:UIControlEventTouchUpInside];
cell.btnForLikeText.tag=s;
cell.btnForCommentText.tag=s;
cell.btnForFavouritetext.tag=s;
[cell.btnForFavouritetext addTarget:self
action:#selector(addFavourite:)
forControlEvents:UIControlEventTouchUpInside];
[cell.btnForCommentText addTarget:self action:#selector(addcomen:)forControlEvents:UIControlEventTouchUpInside];
if([[[arrayForPhotosData1 objectAtIndex:s] valueForKey:#"FavoriteIsActive"] isEqualToString:#"true"])
{
[cell.btnForFavouritetext setTitle:[NSString stringWithFormat:#"%# ",#"Favoritos"] forState:UIControlStateNormal];
}
[cell.btnForFavouritetext setTitle:[NSString stringWithFormat:#"%# ",#"Favoritos"] forState:UIControlStateNormal];
[arrayForCell addObject:cell];
}
[tableViewForPhotos reloadData];
and this is my displaying data in cell
static NSString *simpleTableIdentifier1 = #"TableItem1";
PhotoCell *cell1 = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier1];
if (cell1 == nil) {
cell1 = [[PhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier1];
}
if ([arrayForCell count]>0) {
cell1= [arrayForCell objectAtIndex:indexPath.row-1];
cell1.selectionStyle=UITableViewCellSelectionStyleNone;
return cell1;
}
After loading 50 images, app is getting Received memory warning.

There's no need to check if cell1 is nil. If you registered [PhotoCell class], then it's never going to be nil.
I believe you're receiving a memory warning because you're not reusing cells.
Here:
cell1= [arrayForCell objectAtIndex:indexPath.row-1];
What are you trying to achieve?

Related

How to change Button image added to UITableview section header in ios

Here is my code
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];
button.frame = CGRectMake(sectionView.frame.size.width-10, 10, 20, 20);
[button addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:button];
}
- (void)btnAction:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"Add_minus_iCon_iphone_4s"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"Add_plus_iCon_iphone_4s"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"Add_minus_iCon_iphone_4s"] forState:UIControlStateHighlighted];
}
}
First thing set images of button for both state (UIControlStateNormal, UIControlStateSelected) in ViewDidLoad and then do [cell.btnSelect setSelected:YES]; or [cell.btnSelect setSelected:NO]; as per requirement.
Otherwise change image for UIControlStateNormal when button tapped.
you are creating UIButton each time header load, which cause your button to reset,
1 way is to design cell in storyborad or xib and use reference,
Also have a DataSource to keep track of values
e.g.
if([dataSouce containsObject:yourItem]){
//selected button image
[cell.btnSelect setSelected:YES];
}
else{
//unselected button image
[cell.btnSelect setSelected:NO];
}

conditions checking for every single cell

I am having a table view, in the table cell I have two buttons and checking their conditions. its working but my problem is I want to check the button condition for every single cell in the table view. can anyone help me please.
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"l"ofType:#"png"]] forState:UIControlStateNormal];
button1.tag = indexPath.row;
[button1 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button1];
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
button2.frame = CGRectMake(160, 27, 36, 36);
[button2 setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"e"ofType:#"png"]] forState:UIControlStateNormal];
button2.tag = indexPath.row;
[button2 addTarget:self action:#selector(radiobtn:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button2];
- (void)radiobtn:(UIButton *)button
{
if(btn3 == 0) {
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:#"l.png"]
forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:#"button1"];
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:#"lblue.png"]
forState:UIControlStateSelected];
[button setSelected:YES];
else{
}
}
- (void)radiobtn3:(UIButton *)button
{
if(btn1 == 0)
{
if ([button isSelected]) {
[button setImage:[UIImage imageNamed:#"v.png"]
forState:UIControlStateNormal];
[[NSUserDefaults standardUserDefaults]setBool:NO forKey:#"button3"];
}
[button setSelected:NO];
} else {
[button setImage:[UIImage imageNamed:#"vblue.png"]
forState:UIControlStateSelected];
[button setSelected:YES]; }}
else {
}
conditions are working good. but i want to check the conditions for every single cell . help me in coding.
You need to set tags for every button you allocate depending on your indexPath.row in cellForRowAtIndexPath datasource method of your UITableView. This link might help you out.
Maintain one array for checking the state of UIButton which will be class member.
Add #define for each button to know uniquely which button state we are referring like this :
#define kFirstBtnState #"firstbuttonstate"
#define kSecondBtnState #"secondbuttonstate"
Now initialize array in viewDidLoad like this :
//use tableview object array here
for(int i=0; i<[yourDataSourceOfTableViewArray count]; i++)
{
//Currently each button state is 0 which is false
NSDictionary *dictBtnState = [NSDictionary dictionaryWithObjectsAndKeys:#"0",kFirstBtnState,#"0",kSecondBtnState,nil];
[mutArrBtnStateMaintain addObject:dictBtnState];
}
Use created array in UITableView's cellForRowAtIndexPath like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....................
....................
//get state list of buttons
NSDictionary *dictBtnState = [mutArrBtnStateMaintain objectAtIndex:indexPath.row];
BOOL firstBtnState = [[dictBtnState objectForKey:kFirstBtnState] boolValue];
BOOL secondBtnState = [[dictBtnState objectForKey:kSecondBtnState] boolValue];
//Act according to your requirement
return cell;
}
Note : Replace state in array when necessary to maintain the state of UIButton

Issue with UIButton Title

I am fetching the data from UITableViewCell and am storing into string is countryString. I want to change the button title with country string but its not working, i thought it is a simple issue, but am struggling from last 2 hours. still I cant find the solution why is not working
countryButton=[[UIButton alloc]initWithFrame:CGRectMake(145, 145, 100, 30)];
countryButton.backgroundColor=[UIColor whiteColor];
[countryButton setTitle:CountryString forState:UIControlStateNormal];
countryButton.titleLabel.textColor=[UIColor blackColor];
[countryButton addTarget:self action:#selector(countryTable) forControlEvents:UIControlEventTouchUpInside];
[notesView addSubview:countryButton];
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
jsonDict = [newarray objectAtIndex:indexPath.row];
CountryString=[jsonDict objectForKey:#"countryName"];
NSLog(#"country is %#", CountryString);
CountryTableview.hidden=YES;
}
try this
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
jsonDict = [newarray objectAtIndex:indexPath.row];
CountryString=[jsonDict objectForKey:#"countryName"];
NSLog(#"country is %#", CountryString);
[countryButton setTitle:CountryString forState:UIControlStateNormal];
countryButton.titleLabel.textColor=[UIColor blackColor];
CountryTableview.hidden=YES;
}
assume that its your str
CountryString =#"XXXX";
UIButton *countryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //or button type custom
[countryButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[countryButton setFrame:CGRectMake(145, 145, 100, 30)];
[countryButton setTitle: CountryString forState:UIControlStateNormal];
[countryButton setExclusiveTouch:YES];
countryButton.backgroundColor=[UIColor whiteColor];
countryButton.titleLabel.textColor=[UIColor blackColor];
[self.view addSubview: countryButton]; //whatever u need self.view or notesView
-(void) buttonClicked:(UIButton*)sender
{
NSLog(#"you clicked on button %#", sender.tag);
}

Custom Reusable UITableViewCell

I want to make a custom table cell that handles filling of text fields automatically. My idea is that I'll just pass an object to the cell class and the cell will then automatically fill in the fields. Everything works fine except that no button inside the cell work, they are all unclickable. What am I doing wrong?
Main Screen:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
Claim *claim = [statementsArray objectAtIndex:row];
NSString * strIndentifier;
strIndentifier = #"StatementDetailsCellIdentifier";
StatementDetailsCell *cell = (StatementDetailsCell *) [tableView dequeueReusableCellWithIdentifier:strIndentifier];
cell.hasWarranty = claim.hasWarranty;
if(cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"StatementDetailsCell" owner:self options:nil];
cell = [statementCell initWithClaim:claim reuseIdentifier:strIndentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
return cell;
}
Cell.m :
-(id)initWithClaim:(Claim *)_claim reuseIdentifier:(NSString *)reuseIdentifier
{
self = [self initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
claim = _claim;
[self populate];
return self;
}
-(void)populate
{
barcodeLabel.text = claim.barcode;
NSLog(#"claim is %#", [claim description]);
if(claim.points == 0 || claim.points == 0.00)
valueLabel.text = #"Pending";
else
valueLabel.text = [NSString stringWithFormat:#"£%.2f", claim.points];
modelLabel.text = claim.product;
warrantyLabel.text = claim.warranty.name;
APIRequest *apiRequest = [[APIRequest alloc] init];
dateLabel.text = [apiRequest parseDate:claim.date];
//hasWarranty = claim.hasWarranty;
double timeS = [apiRequest getUnixTimestamp:claim.date];
NSDate *now = [NSDate date];
NSDate *trueDate = [NSDate dateWithTimeIntervalSince1970:timeS];
double timeDiffrece = [now timeIntervalSinceDate:trueDate];
double threemonths = 90*24*3600;
//Warranty Button
if(claim.hasWarranty)
{
UIImage *buttonImage;
//13-6-2013
if([claim.warranty.name isEqualToString:#"Pending"]) {
buttonImage = [UIImage imageNamed:#"imgBtnWarrantyPending.png"];
pendingHelp.hidden = NO;
} else {
pendingHelp.hidden = YES;
buttonImage = [UIImage imageNamed:#"warranty_claimed.png"];
}
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];
[warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
}
else
{
pendingHelp.hidden = YES;
if(false) {
[warrantyButton setBackgroundImage:nil forState:UIControlStateNormal];
[warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
} else {
[warrantyButton setTag:claim.ID];
UIImage *buttonImage = [UIImage imageNamed:#"add_warranty.png"];
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted];
[warrantyButton addTarget:self action:#selector(haha:) forControlEvents:UIControlEventTouchDown];
warrantyButton.userInteractionEnabled = YES;
warrantyButton.enabled = YES;
}
}
}
I believe you're trying to replicate what the Sensible TableView framework already does. I'd recommend you check it out first.
Might be your target would be remove from method . Please assign a break point at the strating of populate method and try to show or hide warrantyButton if target assign for check. and double check name of images.
if(claim.hasWarranty)
{
UIImage *buttonImage;
//13-6-2013
if([claim.warranty.name isEqualToString:#"Pending"]) {
buttonImage = [UIImage imageNamed:#"imgBtnWarrantyPending.png"];
pendingHelp.hidden = NO;
} else {
pendingHelp.hidden = YES;
buttonImage = [UIImage imageNamed:#"warranty_claimed"];
}
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];
[warrantyButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[warrantyButton setHidden:YES];
}
else
{
pendingHelp.hidden = YES;
[warrantyButton setTag:claim.ID];
UIImage *buttonImage = [UIImage imageNamed:#"add_warranty"];
[warrantyButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[warrantyButton setBackgroundImage:nil forState:UIControlStateHighlighted];
[warrantyButton addTarget:self action:#selector(haha:) forControlEvents:UIControlEventTouchDown];
warrantyButton.userInteractionEnabled = YES;
warrantyButton.enabled = YES;
[warrantyButton setHidden:NO];
}
Might be it never goes into else part for assigning target. I hope you understand it better.

Memory leak when loading image to custom cell in UITableView

In my iPhone app, having a UITableView with custom cells. Which contains UILabels and UIImageViews. But I am getting memory leak when I assign the image to the image view. Here is the code. Leak is in the method cellForRowAtIndexPath: I have mentioned the leak percentage. Please check the code, what went wrong here? Please help.
// UIMenuItemCell.h
#class UIMenuitemImage;
#interface UIMenuItemCell : UITableViewCell{
UILabel *cellItemName;
UIImageView *cellitemImage;
}
#property (nonatomic, retain) UILabel *cellItemName;
#property (nonatomic, retain) UIImageView *cellitemImage;
// UIMenuItemCell.m
#import "UIMenuItemCell.h"
#implementation UIMenuItemCell
#synthesize cellItemName, cellitemImage, cellItemButton, cellItemProgress;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
// cellitemImage = [[UIMenuitemImage alloc]init];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
// MenuScreenVC.m
- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 150, 60);
CGRect Label1Frame = CGRectMake(20, 23, 98, 30);
CGRect imgFrame = CGRectMake(20, 48, 110, 123);
CGRect btnFrame = CGRectMake(25, 136, 100, 30);
CGRect progressFrame = CGRectMake(25, 140, 100, 21);
UILabel *lblTemp;
UIImageView *itemImg;
UIButton *itemBtn;
UIProgressView *itemProgView;
UIMenuItemCell *cell = [[[UIMenuItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.frame = CellFrame;
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
lblTemp.textColor=[UIColor colorWithRed:139.0f/255.0f green:69.0f/255.0f blue:19.0f/255.0f alpha:1.0f];
lblTemp.textAlignment = UITextAlignmentCenter;
lblTemp.backgroundColor = [UIColor clearColor];
lblTemp.font = [UIFont systemFontOfSize:13.0];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Initialize ImageView
itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
itemImg.tag = 2;
[cell.contentView addSubview:itemImg];
[itemImg release];
//Initialize Button
itemBtn = [[UIButton alloc]initWithFrame:btnFrame];
itemBtn.frame = btnFrame;
itemBtn.tag = 3;
itemBtn.titleLabel.textColor = [UIColor blueColor];
itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
[cell.contentView addSubview:itemBtn];
[itemBtn release];
//Initialize ProgressView
itemProgView = [[CustomProgressView alloc]initWithFrame:progressFrame];
itemProgView.tag = 4;
//[cell.contentView addSubview:itemProgView];
[itemProgView release];
return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d", indexPath.row];
UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [self getCellContentView:CellIdentifier];
cell.transform = CGAffineTransformMakeRotation(M_PI_2);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.cellItemName = (UILabel *)[cell viewWithTag:1];
cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
cell.cellItemButton = (UIButton *)[cell viewWithTag:3];
cell.cellItemProgress = (UIProgressView *)[cell viewWithTag:4];
DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
__autoreleasing NSString *imageLocalFilePath = nil;
if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"NotAvailable"]) {
cell.cellItemProgress.hidden = YES;
cell.cellItemButton.hidden = NO;
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalNotAvailPath objectAtIndex:indexPath.row]];
NSString *date = [self changeDateFormat:itemObj.itemReleaseDate];
[cell.cellItemButton setTitle:date forState:UIControlStateNormal];
cell.cellItemButton.userInteractionEnabled = NO;
cell.userInteractionEnabled = NO;
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"not_available_bttn_bck_img"] forState:UIControlStateNormal];
}else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Available"]){
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_pressed"] forState:UIControlStateHighlighted];
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton addTarget:self action:#selector(confirmationAlert:) forControlEvents:UIControlEventTouchUpInside];
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
}else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Active"]) {
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton addTarget:self action:#selector(alert) forControlEvents:UIControlEventTouchUpInside];
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
}else if([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Downloading"]) {
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
[cell.contentView addSubview:myprogressView];
cell.cellItemButton.hidden = YES;
}
if ([imageLocalFilePath isEqualToString:#""]) {
[cell.cellitemImage setImage:[UIImage imageNamed:#"item01.png"]];
}else {
[cell.cellitemImage setImage:[UIImage imageWithContentsOfFile:imageLocalFilePath]];
}
cell.cellItemName.text = [NSString stringWithFormat:#"%#",[tempItemNameArray objectAtIndex:indexPath.row]];
for (UIProgressView *prog in cell.contentView.subviews) {
if ([prog isKindOfClass:[UIProgressView class]]){
if (prog.progress == 1) {
[prog removeFromSuperview];
cell.cellItemButton.hidden = NO;
DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
NSString *imageLocalFilePath = nil;
if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Available"]){
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_pressed"] forState:UIControlStateHighlighted];
[cell.cellItemButton addTarget:self action:#selector(confirmationAlert:) forControlEvents:UIControlEventTouchUpInside];
}else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Active"]) {
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateHighlighted];
[cell.cellItemButton addTarget:self action:#selector(alert) forControlEvents:UIControlEventTouchUpInside];
}
imageLocalFilePath = [NSString stringWithFormat:#"%#",itemObj.availableLocalIconPath];
if ([imageLocalFilePath isEqualToString:#""]) {
[cell.cellitemImage setImage:[UIImage imageNamed:#"item01.png"]];
}else {
[cell.cellitemImage setImage:[UIImage imageWithContentsOfFile:imageLocalFilePath]];
}
cell.cellItemName.text = [NSString stringWithFormat:#"%#",[tempItemNameArray objectAtIndex:indexPath.row]];
[cell.contentView reloadInputViews];
}
}
}
}else {
for (UIProgressView *prog in cell.contentView.subviews) {
if ([prog isKindOfClass:[UIProgressView class]]){
if (prog.progress == 1) {
[prog removeFromSuperview];
cell.cellItemButton.hidden = NO;
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateHighlighted];
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton addTarget:self action:#selector(alert) forControlEvents:UIControlEventTouchUpInside];
}
}
}
}
return cell;
}
In your method
- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier
Use:
UIMenuItemCell *cell = [[[UIMenuItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
Edited to respond to comments:
The proper way to release the iVars is in the dealloc method. If it's not getting called, that means your UIMenuItemCells aren't being released. Possibly this is because of the way that your implementation works; you're creating a reusable cell for every row in your table view. I think that if you mark a cell for reuse it doesn't get released until the table view is released.
You could test this by seeing if all the memory is released when your table view is released (if this happens in your app). That would mean that you don't actually have a leak, just that your app uses a lot of memory (most likely unnecessarily). You could also test to see if the memory increases when you scroll rows that you've already created back onto the screen, because in that case I think you will be reusing your cells, so the memory shouldn't increase.
You could also try initializing your table view cells with nil for the cell identifier so that they get released when they scroll off screen.
You should probably try to rework your code though so that you do actually reuse your cells. Otherwise scrolling is most likely going to be pretty choppy.
I meet similar issue, and finally resolve it.
because your table cell would be reused later,
so just call removeFromSuperview before add subviews to the cell.
I had a memory spike issue while loading image to imageView.
below code fixed my issue,
Note: This will reduce the image quality.
In my case i'm loading image in smaller image view.
let resizedImage = image.aspectFittedToHeight(100)
resizedImage.jpegData(compressionQuality: 0.2)
return resizedImage
}
and add this Extension
extension UIImage {
func aspectFittedToHeight(_ newHeight: CGFloat) -> UIImage {
let scale = newHeight / self.size.height
let newWidth = self.size.width * scale
let newSize = CGSize(width: newWidth, height: newHeight)
let renderer = UIGraphicsImageRenderer(size: newSize)
return renderer.image { _ in
self.draw(in: CGRect(origin: .zero, size: newSize))
}
}
}

Resources