Custom Reusable UITableViewCell - ios

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.

Related

section Header not selectable

In my Last Question extension Now I am able to select rows in a particular section but How section will select using buttons
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
Header *headerView = [tableView dequeueReusableCellWithIdentifier:#"HeaderView"];
UILabel *name = (UILabel*) [headerView.contentView viewWithTag:section+2];
UILabel *code = (UILabel*) [headerView.contentView viewWithTag:section+4];
name.text = [_data[section] valueForKey:#"Name"] ;
code.text=[_data[section] valueForKey:#"Code"] ;
imagebutton=(UIButton*)[headerView.contentView viewWithTag:section+3];
UIImage *btnImage = [UIImage imageNamed:#""];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor whiteColor]];
if(headerArray.count>0)
{
if([headerArray containsObject:#"0"])
{
UIImage *btnImage = [UIImage imageNamed:#""];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor whiteColor]];
}
else
{
UIImage *btnImage = [UIImage imageNamed:#"ic_floating_done_#1x"];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
}
}
UIButton *btn=(UIButton*)[headerView.contentView viewWithTag:section+1];
[btn addTarget: self
action: #selector(buttonClicked:event:)
forControlEvents: UIControlEventTouchUpInside];
return headerView;
}
This is My header view here I am using button to select section
(void)buttonClicked:(id)sender event:(id)event
{
if(imagebutton.currentImage == [UIImage imageNamed:#""] )
{
UIImage *btnImage = [UIImage imageNamed:#"ic_floating_done_#1x"];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor colorWithRed:0/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
}
else
{
UIImage *btnImage = [UIImage imageNamed:#""];
[imagebutton setImage:btnImage forState:UIControlStateNormal];
[imagebutton setBackgroundColor:[UIColor whiteColor]];
}
}
TableView CheckMarks
For details, I am giving my last question link here my data and tableview delegates available
My problem is I am not able to select the first section if I click any row two sections are selected
If particular section row selects that section only select and if section select all rows will select.
Please give a clear example for this.
Try to connect the header UIView to a property in your .h file.

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];
}

Tableview loading more than 50 images Received memory warning

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?

UIbutton image changes to default

I have a Uibutton and its default background images is set i changes its image at run time but i want to preserve the selected image. but each time i click on uibutton image changes to default. Here is the code
- (void)changeColorOnRuntTime:(UIColor *)color{
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateNormal];
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateHighlighted];
// [btnTmpForColorPicker seti];
// tmpColor = [UIColor alloc]ini;
tmpColor = [color copy];
// NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// [defaults setValue:color forKey:#"CustomPreColor"];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:color] forKey:#"CustomPreColor"];
}
and in btnHandler
if (popOverColorPicker.popoverVisible) {
[popOverColorPicker dismissPopoverAnimated:YES];
}
else
{
sdViewController = [[SDColorPickerViewController alloc]init];
sdViewController.color = [UIColor whiteColor];
sdViewController.tag = 1;
sdViewController.delegate = self;
// CGRect rect = CGRectMake(viewController.view.frame.origin.x, viewController.view.frame.origin.y, viewController.view.frame.size.width, 380);
// viewController.view.frame = rect;
btnTmpForColorPicker = (UIButton*)sender;
UIButton *senderbt = (UIButton*) sender;
if (tmpColor) {
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:tmpColor] forState:UIControlStateNormal];
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:tmpColor] forState:UIControlStateHighlighted];
}
// [popOverColorPicker setPopoverContentSize:CGSizeMake(viewController.view.frame.size.width, 380)];
popOverColorPicker = [[UIPopoverController alloc]initWithContentViewController:sdViewController];
[popOverColorPicker setPopoverContentSize:CGSizeMake(300, 390)];
[popOverColorPicker presentPopoverFromRect:senderbt.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
Replace this:
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateHighlighted];
with this:
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateHighlighted] || UIControlStateSelected];
And apply the same change to the btnHandler button

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