I used an UINavigationController pushViewController to present an ViewController, on the view of the viewcontroller, there is an UITablView.
I used the codes below to draw UILabel on UITableViewCell, but it does not display, only when I tap the row where the UIlabel is, it will appear, if I tap another row, it disappear again.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier1 = #"CellTableIdentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier1 ];
if (cell == nil)
{
cell=[[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:SimpleTableIdentifier1] autorelease];
switch([[myArray objectAtIndex:indexPath.row] getValueType])
{
case _PSToggleSwitchSpecifier:
{
UISwitch *switchview=[[UISwitch alloc]init];
switchview.tag=11106 ;
cell.accessoryView= switchview;
//switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
//[cell.contentView addSubview:switchview];
[switchview addTarget:self action:#selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[switchview release];
break;
}
case _PSMultiValueSpecifier:
{
cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;//
//draw a label, but actually it does display,
UILabel *labelCell =[ [UILabel alloc]init];
[labelCell setTag:11101];
labelCell.frame = CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
[labelCell setBackgroundColor:[UIColor clearColor]];
labelCell.textAlignment=UITextAlignmentRight;
[labelCell setFont:[UIFont systemFontOfSize:18.0]];
[labelCell setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:labelCell];
[labelCell release];
break;
}
}
}
cell.text=#"test";
switch([[myArray objectAtIndex:indexPath.row] getValueType])
{
case _PSToggleSwitchSpecifier:
{
cell.accessoryType=UITableViewCellAccessoryNone;//
UISwitch *tem=(UISwitch*) [cell.contentView viewWithTag:11106];//cell.accessoryView;//
tem.on= true;
break;
}
case _PSMultiValueSpecifier:
{
UILabel *newLabelCell=(UILabel*)[cell.contentView viewWithTag: 11101];//intV];
newLabelCell.text=#"Items";//it should display, but actually not, only when I tap the row "didselect" the text display
break;
}
};
return cell;
}
In codes above UISwitch are displayed, but if I use codes
switchview.frame=CGRectMake(80.9f,15.0f,80.0f,23.0f) ;
[switchview addTarget:self action:#selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switchview];
to replace cell.accessoryView= switchview;
UISwitch will be the same thing as UILabel
Welcome any comment
Thanks
interdev
Try using initWithStyle to create your UITableViewCell...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
Note: initWithFrame deprecated, and you're using a CGRectZero for the frame
Related
I need help with this. I have updated xcode to the latest and when I try to get test, the old code throws an error "class is not key value coding-compliant for the key scrollView."
Funny thing is that there is no scrollview in this. I have other views almost exact code and it works fine. Here is the code. there is no nib for this either so the taking out the scrollview from the view won't work.
#import "pearGalleryController.h"
#import "pearGallery.h"
#implementation pearGalleryController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:100];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.navigationItem.title = #"Variety of Pears";
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 122;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// cell is an object of the UITableViewCell this assign an object to a cell 2 memory areas, o need to auto release on the alloc bcause it is an object
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// Set up the cell...
int picIndex = [indexPath row]*3;
UIButton* tempView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 107,123)];
[tempView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"pears_%d.png",picIndex]]
forState:UIControlStateNormal];
tempView.tag = picIndex;
[tempView addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:tempView];
[tempView release];
tempView = [[UIButton alloc]initWithFrame:CGRectMake(106, 0, 107,123)];
[tempView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"pears_%d.png",picIndex+1]]
forState:UIControlStateNormal];
tempView.tag = picIndex+1;
[tempView addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:tempView];
[tempView release];
tempView = [[UIButton alloc]initWithFrame:CGRectMake(212, 0, 107,123)];
[tempView setImage:[UIImage imageNamed:[NSString stringWithFormat:#"pears_%d.png",picIndex+2]]
forState:UIControlStateNormal];
tempView.tag = picIndex+2;
[tempView addTarget:self action:#selector(buttonClick:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:tempView];
[tempView release];
return cell;
}
-(void)buttonClick:(id)sender
{
UIButton* btn = (UIButton*)sender;
int index = btn.tag;
pearGallery *anotherViewController = [[pearGallery alloc] initWithNibName:#"pearGallery" bundle:[NSBundle mainBundle]];
anotherViewController.myIndex = index;
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
//UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"video.png"] style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[super dealloc];
}
#end
and the h file
#import
#interface pearGalleryController : UITableViewController {
}
#end
probably a dangling reference in your xib file, check to make sure references are cleared.
Usually this error occurs when you have an object , in your case it is the 'scrollView' which is attached via the interface builder to the scrollView item in storyboard/xib.
Check your links in interface builder ( furthest tab to the right ) and that you don't have a duplicate link to another IBOutlet.
I find this only occurs to me when I link an object up then 'mistype' my link , I go to change it but don't remove the old link from interface builder ie SCrollview and scroLLview2
Go to your interface builder ( connection tab ) and check you don't have duplicate links.
Remove all links to your 'scrollview' and 're link' to your .h file then try again
you may have typed scrollView then changed it to scrollview and not realised..good luck
I have seen some posts before, but didn't get the answer yet, thats why i am trying to post again in more effective manner. How can i use check-uncheck functionality in UITableView like below image.
This is table i want when i click on button of any cell, that buttons image will change, not on all cells.
For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh.
And i saw your previous question you're adding two buttons with two action not a good way just change the image of button for check-uncheck.
So here is what i do for this -
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tblView;
NSMutableArray *arrayCheckUnchek; // Will handle which button is selected or which is unselected
NSMutableArray *cellDataArray; // this is your data array
}
#end
Now in ViewController.m class -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
arrayCheckUnchek = [[NSMutableArray alloc]init];
//Assign your cell data array
cellDataArray = [[NSMutableArray alloc]initWithObjects:#"cell-1",#"cell-2",#"cell-3",#"cell-4",#"cell-5", nil];
// setting all unchecks initially
for(int i=0; i<[cellDataArray count]; i++)
{
[arrayCheckUnchek addObject:#"Uncheck"];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [cellDataArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
[button setImage:[UIImage imageNamed:#"uncheck_icon"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:#"check_icon"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
return cell;
}
-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
//Checking the condition button is checked or unchecked.
//accordingly replace the array object and change the button image
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{
[button setImage:[UIImage imageNamed:#"check_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Check"];
}
else
{
[button setImage:[UIImage imageNamed:#"uncheck_icon"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Uncheck"];
}
}
And finally it will look like -
Tags are useful with customised uitableviewcell designed in IB. If you create cells programmatically, you don't need tags. You use them only to find correct uiview to set properties.
Yes this is simple to set Tags while declaring the classes inside UITableViewCell and identify them using tags. I have posted some sample code for your reference. Am not sure it will solve your problem. You asked how to set tags and identify the classes using tags. So it will give you some basic idea about your question.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
label1 = [[UIView alloc] initWithFrame:CGRectMake(2, 20, 15, 15)];
label1.tag = 100;
label1.backgroundColor = [UIColor whiteColor];
label1.layer.cornerRadius = 5;
[cell.contentView addSubview: label1];
label2 = [[UILabel alloc] initWithFrame:CGRectMake(25, 2, 165, 23)];
label2.tag = 101;
label2.font = [UIFont boldSystemFontOfSize:15];
label2.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview: label2];
label3 = [[UILabel alloc] initWithFrame:CGRectMake(190, 2, 90, 23)];
label3.tag = 102;
label3.font = [UIFont systemFontOfSize:14];
label3.textColor = [UIColor colorWithRed:0.14117670588235 green:0.435294117647059 blue:0.847058823529412 alpha:1];
label3.backgroundColor = [UIColor clearColor];
label3.textAlignment = UITextAlignmentRight;
[cell.contentView addSubview: label3];
}
label1 = (UILabel *) [cell.contentView viewWithTag:100];
NSString *string1 = [array1 objectAtIndex:indexPath.row];
label2 = (UILabel *) [cell.contentView viewWithTag:101];
NSString * string2 = [array2 objectAtIndex:indexPath.row];
label3 = (UILabel *) [cell.contentView viewWithTag:102];
NSString * string3 = [array3 objectAtIndex:indexPath.row];
return cell;
}
Please let me know this is useful or not. Otherwise we'll go to some other way.
Happy Coding. Thanks.
i agree with #VakulSaini because u can do this to handle which cell touch or swipe or what ever and this is an ex:
-(void)handleSwipLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
CGPoint Location = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:Location];
cell = [tableView cellForRowAtIndexPath:indexPath];
}
Use cellForRowAtIndexPath method of UITableView and in This method Add any component like (UITextField,UILabel ..... etc ) In your UITableViewCell by using [cell.contentView addSubview:YourComponentName]; and give each component to its tag by indexPath.row.
such like ,,,
YourComponentName.tag = indexPath.row;
I implemented the following method in my tableviewcontroller for showing favorites with empty star and filled star:
-(IBAction) tapped:(UIButton *)sender
{
if([sender isSelected])
{
//...
[sender setImage:[UIImage imageNamed:#"fav-empty.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else
{
//...
[sender setImage:[UIImage imageNamed:#"fav-filled.png"] forState:UIControlStateNormal];
[sender setSelected:YES];
NSLog(#"%#",sender);
}
}
and I tried to use in this part:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UILabel *recept_neve=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
[recept_neve setTag:1];
UIButton *kedvenc_btn=[UIButton buttonWithType:UIButtonTypeCustom];
[kedvenc_btn setImage:[UIImage imageNamed:#"fav-empty.png"] forState:UIControlStateNormal];//default appereance on the startup
kedvenc_btn.frame=CGRectMake(200, 20, 50, 50);
[kedvenc_btn setTag:2];
[kedvenc_btn addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
[[cell contentView] addSubview:recept_neve];
[[cell contentView] addSubview:kedvenc_btn];
//[(UILabel*) [[cell contentView] viewWithTag:1] setText:[receptek objectAtIndex:indexPath.section*blokk+1]];
//[(UIButton *) [cell contentView] viewWithTag:2];
}
// Configure the cell...
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
[(UILabel*) [[cell contentView] viewWithTag:1] setText:[receptek objectAtIndex:indexPath.section*blokk+1]];
return cell;
}
My problem is that every fifth button in the cells are working as would be the same.
If I tap on the button(empty star) in the first cell, the result will be the following: the first button becoming filled-star, so far so good(the expected behaviour), but in the same time the sixth star becoming filled too. Or if I tap on the second one, the seventh star getting filled and vica versa...
I logged the buttons where I tapping , and I realised, that the memory adresses are equals in the case of buttons above mentioned.
Sorry about my english, and thanks a lot for every help in advance!
the problem was that you reused the buttons all the time.
I show you a sample working code to structure a customized UITableViewCell correctly...
Let's do it step by step.
Create a new class called MyObjTableViewCellForSelection and provide these methods. Adapt it to your needs!
#class MyObj;
#interface MyObjTableViewCellForSelection : UITableViewCell
{
MyObj *myObj;
UIImageView *imageView;
UILabel *titleLabel;
}
#property (nonatomic,strong) MyObj *myObj;
#property (nonatomic,strong) UIImageView *imageView;
#property (nonatomic,strong) UILabel *titleLabel;
#end
Then the implementation:
#implementation MyObjTableViewCellForSelection
#synthesize myObj;
#synthesize imageView;
#synthesize titleLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
imageView = [[UIImageView alloc] initWithFrame: CGRectZero];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview: imageView];
titleLabel = [[UILabel alloc] initWithFrame: CGRectZero];
[titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[titleLabel setTextColor:[UIColor blackColor]];
[titleLabel setHighlightedTextColor:[UIColor darkGrayColor]];
[titleLabel setLineBreakMode: UILineBreakModeWordWrap];
titleLabel.numberOfLines = 2;
[self.contentView addSubview: titleLabel];
}
return self;
}
-(void) layoutSubviews {
[super layoutSubviews];
[imageView setFrame:CGRectMake(8.0, 10.0, 20.0, 20.0)];
[titleLabel setFrame:CGRectMake(40.0, 1.0, 250.0, 40.0)]; //two lines
}
- (void)setMyObj:(MyObj *)myObjX {
if (myObjX != myObj) {
[myObj release];
[myObjX retain];
//myObj = myObjX; //only with ARC on
}
titleLabel.text = whatever you need accessing myObj
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// selection has to be adapted to your structure
[super setSelected:selected animated:animated];
switch (myObj.selectedFlag) {
case MYOBJ_STATUS_UNSELECTED:
imageView.image = [UIImage imageNamed:#"EmptyBullet.png"];
break;
case MYOBJ_STATUS_SELECTED:
imageView.image = [UIImage imageNamed:#"GreyBullet.png"];
break;
default:
break;
}
#end
now in your view class you can finally do:
- (MyObj *)myObjForIndexPath:(NSIndexPath *)indexPath {
MyObj* tmpObj = get the corresponding object
return tmpObj;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyObjTableViewCellForSelection *cell = (MyObjTableViewCellForSelection *)[tableView dequeueReusableCellWithIdentifier:#"cellID"];
if (cell == Nil) {
cell = [[ECUTableViewCellForSelection alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cellID"];
}
cell.myObj = [self myObjForIndexPath:indexPath]; //the assignment calls setMyObj of CLASS ECUTableViewCellForSelection that you just declared
return cell;
}
So finally that's the clean way of doing it...
My problem is I have a Custom button and ImageView to which I added the Image.I made this imageView to the custom button as a subview.when Im running it in simulator it workin fine,but when Im runnig it in Device it was not displaying the image in the button.
I written the code as:
In cellForRowtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MasterViewIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MasterViewIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView* elementView = [[UIView alloc] initWithFrame:CGRectMake(20,170,320,280)];
elementView.tag = 0;
elementView.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:elementView];
[elementView release];
}
UIView* elementView = [cell.contentView viewWithTag:0];
elementView.backgroundColor=[UIColor clearColor];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
if(indexPath.section == 8)
{
imageView.image = [UIImage imageNamed:#"yellow_Star.png"];
MyCustomButton *button1 = [[MyCustomButton alloc]initWithRating:1];
[button1 setFrame:CGRectMake(159, 15, 25, 20)];
[button1 setShowsTouchWhenHighlighted:YES];
[button1 addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button1 addSubview:imageView];
[elementView addSubview:button1];
[button1 release];
[imageView release];
}
return cell;
}
and in my buttonAction:
-(void)buttonAction:(MyCustomButton*)sender
{
rating = [sender ratingValue];
event.eventRatings = rating;
[tableView reloadData];
}
and i MyCustomButton class:
i have a method as
-(MyCustomButton*) initWithRating:(NSInteger)aRatingValue
{
if (self = [super init])
{
self.ratingValue = aRatingValue;
}
return self;
}
pls help guys from dis problem.
Thank you,
Monish.
You're trying to load image named "yellow_Star.png" - check if it actually named so (with the same characters case). File system on iPhone is case sensitive and on Mac OS is not - so it is often causes such problems and file names is the first thing to look at.
Edit: You can also try to check if the image is present in the application bundle - may be it was removed from copy resources build step by chance, that is your image must be in a "Copy Bundle Resources" build step for your build target. If it is absent you can just drag and drop your image there.
I am displaying some data using a UITableViewController, my table has 2 static sections with 6 static rows each. And I am subclassing UITableViewCell in order to add 3 Labels and a View, in the view I draw an arrow in only one of the cells in one of the sections.
This all goes perfectly fine. Till i scroll down... then the arrow is randomly placed in other cells, once i scroll back up the arrow has again changed cells... This also happens if i try to set a backgroundColor for only one of the cells. Once I scroll down and up the cell that originally had the color no longer has it, and another seemingly random cell has the color now.
Here is some of my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCells *cell = (TableViewCells *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCells alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(TableViewCells *)cell atIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
{
if (indexPath.section == cotizacionIdeal)
{
cell.arrowImage = [UIImage imageNamed:#"arrowUp.png"];
[cell.nombre setText:#"Recursos"];
[cell.cantidad setText:[NSString stringWithFormat:#"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]];
[cell.porcentaje setText:[NSString stringWithFormat:#"%1.2f%#", [cotizacion.recursosPorcentaje doubleValue], #"%"]];
}
else
{
[cell.nombre setText:#"Recursos"];
[cell.cantidad setText:[NSString stringWithFormat:#"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]];
[cell.porcentaje setText:[Calculate calcPorcentaje:cotizacionPrevia.sueldoDeRecursos totalReal:self.totalReal]];
}
}
break;
case 1:
{
if (indexPath.section == cotizacionIdeal)
{
[cell.nombre setText:#"Comision de Venta"];
[cell.cantidad setText:[Calculate calcMonto:cotizacion.comisionPorcentaje total:self.totalIdeal]];
[cell.porcentaje setText:[NSString stringWithFormat:#"%1.2f%#", [cotizacion.comisionPorcentaje doubleValue], #"%"]];
}
else
{
[cell.nombre setText:#"Comision de Venta"];
[cell.cantidad setText:self.montoRealComision];
[cell.porcentaje setText:[NSString stringWithFormat:#"%1.2f%#", [cotizacion.comisionPorcentaje doubleValue], #"%"]];
}
}
UITableViewCell class:
- (id)initWithFrame:(CGRect)frame cell:(TableViewCells *)cell
{
if (self = [super initWithFrame:frame]) {
_cell = cell;
//self.opaque = YES;
//self.backgroundColor = _cell.backgroundColor;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[_cell.arrowImage drawAtPoint:CGPointMake(0, 0)];
}
#end
#implementation TableViewCells
#synthesize nombre;
#synthesize porcentaje;
#synthesize cantidad;
#synthesize arrowImage;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// Initialization code
UILabel *nombreLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.nombre = nombreLabel;
[nombreLabel release];
[self.nombre setFont:[UIFont boldSystemFontOfSize:14]];
[self.contentView addSubview:self.nombre];
UILabel *porcentajeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.porcentaje = porcentajeLabel;
[porcentajeLabel release];
[self.porcentaje setFont:[UIFont italicSystemFontOfSize:10]];
[self.contentView addSubview:self.porcentaje];
UILabel *cantidadLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.cantidad = cantidadLabel;
[self.cantidad setTextAlignment:UITextAlignmentRight];
[cantidadLabel release];
[self.cantidad setFont:[UIFont boldSystemFontOfSize:14]];
[self.contentView addSubview:self.cantidad];
cellContentView = [[TableViewCellContentView alloc] initWithFrame:CGRectZero cell:self];
cellContentView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:cellContentView];
}
UPDATE:
I have also tried setting the image to a different one for everyone but one and I am still having the same issue.
- (void)configureCell:(TableViewCells *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.arrowImage = [UIImage imageNamed:#"arrowDown.png"];
switch (indexPath.row) {
case 0:
{
if (indexPath.section == cotizacionIdeal)
{
cell.arrowImage = [UIImage imageNamed:#"arrowUp.png"];
[cell.nombre setText:#"Recursos"];
[cell.cantidad setText:[NSString stringWithFormat:#"%1.2f", [cotizacionPrevia.sueldoDeRecursos doubleValue]]];
[cell.porcentaje setText:[NSString stringWithFormat:#"%1.2f%#", [cotizacion.recursosPorcentaje doubleValue], #"%"]];
}
else
{
The problem is that whenever you use drawRect for a cell you MUST call setneedsdisplay. That is what fixed my problem.
The issue is that the UITableView reuses cells so that it doesn't need to make more instances than are displayed and you're not explicitly removing the arrow in your configureCell method.
If the arrow shouldn't be displayed on a cell, explicitly clear it out in the configureCell method and that should fix it.
I am not 100% on this but I think it is to do with the release of the image. When you use imageNamed, the instance is retained so it is suggested to use imageWithFilePath instead.
Hope this helps....