I put multiple UILabels inside every cell in a UITableView instead of a single cell.textLabel.text. I then use reloaddata to put new uilabels. How do i get rid of the old labels ?
edit: If i put 5 labels in a cell then reload the cell using only 2 labels, there are 3 more labels left over from the last time i called cellForRowAtIndexPath.
If i use viewWithTag like Goldeen said, i can reuse old labels but can i remove labels i dont want from memory ?
edit:
this is my method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(j*50.0, 0, 49.0,logicTable.rowHeight)] autorelease];
label.tag = 1;
label.text = [NSString stringWithFormat:#"ABC"];
label.textAlignment = UITextAlignmentCenter;
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
return cell;
}
What it sounds like you are doing is, in your cellForRowAtIndexPath method, you are setting up your UITableViewCells with some labels in them and each time, you are making the labels from scratch. What you should be doing is, setting up the labels if you are making a new cell, and then setting the values on the labels outside of this to fully utilize the ability to reuse table view cells to improve performance of scrolling the table view.
The key method is -viewWithTag: which, along with the tag property on UIView you can use to find a specific subview.
A little sample code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCellIdentifier";
UITableViewCell *cell = (WHArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *firstLabel = nil;
UILabel *secondLabel = nil;
UILabel *thirdLabel = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
firstLabel = [[[UILabel alloc] initWithFrame: CGRectMake(0.0, 0.0, 20.0, 20.0)] autorelease];
firstLabel.tag = 1;
[cell addSubview:firstLabel];
secondLabel = [[[UILabel alloc] initWithFrame: CGRectMake(20.0, 0.0, 20.0, 20.0)] autorelease];
secondLabel.tag = 2;
[cell addSubview:secondLabel];
thirdLabel = [[[UILabel alloc] initWithFrame: CGRectMake(40.0, 0.0, 20.0, 20.0)] autorelease];
thirdLabel.tag = 3;
[cell addSubview:thirdLabel];
}
else
{
firstLabel = (UILabel *)[cell viewWithTag:1];
secondLabel = (UILabel *)[cell viewWithTag:2];
thirdLabel = (UILabel *)[cell viewWithTag:3];
}
firstLabel.text = #"First Label's Text Here";
secondLabel.text = #"Second Label's Text Here";
thirdLabel.text = #"Third Label's Text Here";
return cell;
}
Related
I have a grouped style Tableview which have uilabels in Tableviewcell. Now i want to set height of uilabels equal to height of cell how can i do ths???
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// here i want to make height of label equal to height of cell
UILabel *category = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,25)];
category.font = [UIFont fontWithName:#"Arial" size:14.0f] ;
category.textAlignment = NSTextAlignmentRight;
[category setBackgroundColor:[UIColor clearColor]];
[cell addSubview:category];
}
In the cellForRowAtIndexPath add; this will return current height set for your tableview:
CGFloat currentCellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
UILabel myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, customWidth, currentCellHeight)];
Get default cell height
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
UILabel *category = [[UILabel alloc]
initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.width)];
If you want your cell's height, just do the following:
category.frame = CGRectMake(0,0,width,cell.bounds.size.height);
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Look at this
UILabel *category = [[UILabel alloc]
initWithFrame:CGRectMake(0,0,cell.bounds.size.width,cell.bounds.size.height)];
category.font = [UIFont fontWithName:#"Arial" size:14.0f] ;
category.textAlignment = NSTextAlignmentRight;
[category setBackgroundColor:[UIColor clearColor]];
[cell addSubview:category];
}
use this code...
YOu can use following to find height in
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize textSize = [[NSString stringWithFormat:#"%# %#",label.text] sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(200, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
return textSize.height; //height for cell
}
I think there's two things you can do; first try reading this link from apple and review your approach. If you only need a label, why not use the cells textLabel and customize its appearance?
If you really want to use the label approach then consider adding it to the contentView instead of the cell. You can also try to get the bounds of this property, instead of the c
UILabel *category = [[UILabel alloc] initWithFrame:CGRectMake(0,0,width,cell.contentView.bounds.size.height)];
[cell.contentView addSubview:category];
Hope this helps.
// try this code
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(95,1,140,cell.frame.size.height)];
just set the cell bounds to label's frame,like below.
UILabel *myLabel = [[UILabel alloc] initWithFrame:cell.bounds];
I have used Cell.ContentView in my implementation for customizing the cell contents. It works fine but the only problem is when I have many cells and I scroll them up and down, the cell contents gets overwritten into the cells just become hidden followed by visible. Suppose I scroll first cell up and then again takes it down, the last cell's contents gets overwritten on first cell!!
I debugged enough on this but couldn't find the exact solution. I tried checking Cell.ContentView.SubViews count and if it 0 then only add other subviews. This doesn't display any cell contents until I scroll them up and down but once contents appeared, it doesn't overwrite..Little bit strange..!! I also made sure that I am using reusing the cell correctly. Following is my code that adds subviews into cell's contentview. Please let me know how could I get rid of this issue.
P.S: Don't worry about the variables and calculations I have done. Assume that it returns correct values. :)
- (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];
}
NSInteger totalAvailableSpace = IPHONE_DISPLAY_WIDTH - iconSize - accesorySize - 10;
NSInteger lableHeight = [[cellDetails objectForKey:#"TableItemTextFontSize"] intValue] * 2 + 10;
UILabel *textLabel = nil;
textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+12, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.8, lableHeight)] autorelease];
textLabel.numberOfLines = 2;
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.textAlignment = UITextAlignmentLeft;
textLabel.text = [cellDetails objectForKey:#"TableItemMainText"];
if ([textLableColor scanHexInt:&hex]) {
textLabel.textColor = UIColorFromRGB(hex);
}
textLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemTextFontName"] size:[[cellDetails objectForKey:#"TableItemTextFontSize"] intValue]];
[cell.contentView addSubview:textLabel];
textLabel.backgroundColor = [UIColor clearColor];
lableHeight = [[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue] * 2 + 10;
UILabel *detailTextLabel = nil;
detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+10+totalAvailableSpace * 0.8+5, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.2 - 10, lableHeight)] autorelease];
detailTextLabel.numberOfLines = 2;
detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
detailTextLabel.textAlignment = UITextAlignmentLeft;
detailTextLabel.text = [cellDetails objectForKey:#"TableItemDetailText"];
if ([detailTextLableColor scanHexInt:&hex]) {
detailTextLabel.textColor = UIColorFromRGB(hex);
}
detailTextLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemDetailTextFontName"] size:[[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue]];
[cell.contentView addSubview:detailTextLabel];
detailTextLabel.backgroundColor = [UIColor clearColor];
return cell;
}
Thanks.
That is because toy are adding the views to your cell over and over again.
You should only add them when you create the cell and just set the labels when the cell is being reused.
You could set tags for your label, so that you can set it texts afterwards. The code bellow does the trick
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if(style == UITableViewCellStyleValue1)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
else
cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:CellIdentifier] autorelease];
NSInteger totalAvailableSpace = IPHONE_DISPLAY_WIDTH - iconSize - accesorySize - 10;
NSInteger lableHeight = [[cellDetails objectForKey:#"TableItemTextFontSize"] intValue] * 2 + 10;
UILabel *textLabel = nil;
textLabel.tag = 1;
textLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+12, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.8, lableHeight)] autorelease];
textLabel.numberOfLines = 2;
textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.textAlignment = UITextAlignmentLeft;
textLabel.text = [cellDetails objectForKey:#"TableItemMainText"];
if ([textLableColor scanHexInt:&hex]) {
textLabel.textColor = UIColorFromRGB(hex);
}
textLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemTextFontName"] size:[[cellDetails objectForKey:#"TableItemTextFontSize"] intValue]];
[cell.contentView addSubview:textLabel];
textLabel.backgroundColor = [UIColor clearColor];
lableHeight = [[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue] * 2 + 10;
UILabel *detailTextLabel = nil;
detailTextLabel.tag = 2;
detailTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(iconSize+10+totalAvailableSpace * 0.8+5, self.tableCellHeight/2 - lableHeight/2, totalAvailableSpace * 0.2 - 10, lableHeight)] autorelease];
detailTextLabel.numberOfLines = 2;
detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
detailTextLabel.textAlignment = UITextAlignmentLeft;
detailTextLabel.text = [cellDetails objectForKey:#"TableItemDetailText"];
if ([detailTextLableColor scanHexInt:&hex]) {
detailTextLabel.textColor = UIColorFromRGB(hex);
}
detailTextLabel.font = [UIFont fontWithName:[cellDetails objectForKey:#"TableItemDetailTextFontName"] size:[[cellDetails objectForKey:#"TableItemDetailTextFontSize"] intValue]];
[cell.contentView addSubview:detailTextLabel];
detailTextLabel.backgroundColor = [UIColor clearColor];
} else {
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.text = [cellDetails objectForKey:#"TableItemMainText"];
UILabel *detailTextLabel = (UILabel *)[cell viewWithTag:2];
detailTextLabel.text = [cellDetails objectForKey:#"TableItemDetailText"];
}
return cell;
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
Make sure that dequeueReusableCellWithIdentifier and reuseIdentifier should be nil
Now it will work !!
Place all the cell UI content inside if(cell==nil){uilabel,uilabel or anythingUI related} ...since ui should be call only once at time of creating cel
To be clear, your problem is that as you scroll a cell into view you will often wind up with the text of the new cell being written over the top of a cell that was just scrolled offscreen?
The problem is that dequeueReusableCellWithIdentifier: doesn't "clean up" the cell (it calls prepareForReuse, but that's it), so all your old subviews are still in place. If you are going to reuse cells, you shouldn't be recreating these subviews each time. Instead, just adjust the properties of the existing subviews on a cell you get back from dequeueReusableCellWithIdentifier:, and only create the new subviews on a newly-allocated cell. Subclassing UITableViewCell can help with this, both to provide properties/ivars in which to hold references to these subviews and to organize the code a little nicer by moving the creation of subviews and the updating into appropriate methods. Or you could just not reuse cells, by passing nil for the reuse identifier.
The most likely reason that it didn't display any content until you scroll up and down is that a newly-created cell may have the framework-provided text field and image view, which may then be removed if the framework determines that you aren't actually using them.
In iOS5 UILineBreakModeWordWrap is deprecated. You should use NSLineBreakByWordWrapping instead.
This would change you code to look like detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping and textLabel.lineBreakMode = NSLineBreakByWordWrapping.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 300, 22)];
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 300, 22)];
UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 22)];
myLabel1.text=aBook.title;
myLabel2.text=aBook.description;
myLabel3.text=aBook.pubDate;
[cell addSubview:myLabel1];
[cell addSubview:myLabel2];
[cell addSubview:myLabel3];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
I am having this code. It displays from XML file. When I scroll, the text gets overlapped. Please help me out.
You add labels to your cell each time cell is reused so you end with multiple labels stacked on each other in one cell. What you need to change is to create labels only when cell itself is being created:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 300, 22)];
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, 300, 22)];
UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 22)];
myLabel1.tag = 101;
myLabel2.tag = 102;
myLabel3.tag = 103;
[cell.contentView addSubview:myLabel1];
[cell.contentView addSubview:myLabel2];
[cell.contentView addSubview:myLabel3];
[myLabel1 release];
[myLabel2 release];
[myLabel3 release];
}
// Configure the cell.
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
UILabel *myLabel1 = (UILabel*)[cell.contentView viewWithTag:101];
UILabel *myLabel2 = (UILabel*)[cell.contentView viewWithTag:101];
UILabel *myLabel3 = (UILabel*)[cell.contentView viewWithTag:101];
myLabel1.text=aBook.title;
myLabel2.text=aBook.description;
myLabel3.text=aBook.pubDate;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
Two more things to fix:
Do not forget to release labels you create after adding them to the cell, otherwise you get memory leak and may eventually run into low memory problems (especially with tableview)
add subviews to cell's contentView, not to the cell directly
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#""];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.backgroundView = [[[CustomCell alloc] init] autorelease];
cell.selectedBackgroundView = [[[CustomCell alloc] init] autorelease];
// At end of function, right before return cell:
cell.textLabel.backgroundColor = [UIColor clearColor];
// Configure the cell.
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 45)];
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 55, 300, 20)];
UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 68, 300, 60)];
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
myLabel1.text=aBook.title;
myLabel2.text=aBook.pubDate;
myLabel3.text=aBook.description;
//myLabel1.lineBreakMode=UILineBreakModeCharacterWrap;
myLabel1.lineBreakMode=UILineBreakModeWordWrap;
myLabel1.numberOfLines=1;
myLabel1.textColor=[UIColor redColor];
myLabel1.backgroundColor = [UIColor blueColor];
myLabel1.font=[UIFont systemFontOfSize:14];
myLabel2.font=[UIFont systemFontOfSize:12];
myLabel3.textAlignment=UITextAlignmentLeft;
myLabel3.textColor=[UIColor blueColor];
myLabel3.lineBreakMode=UILineBreakModeCharacterWrap;
myLabel3.numberOfLines=3;
//myLabel3.lineBreakMode=UILineBreakModeWordWrap;
myLabel3.lineBreakMode=UILineBreakModeTailTruncation;
myLabel3.font=[UIFont systemFontOfSize:14];
//myLabel1.shadowColor=[UIColor redColor];
//myLabel1.backgroundColor=[UIColor grayColor];
[cell.contentView addSubview:myLabel1];
[cell.contentView addSubview:myLabel2];
[cell.contentView addSubview:myLabel3];
//cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[myLabel1 release];
[myLabel2 release];
[myLabel3 release];
//Set up the cell
return cell;
guys i have these codings. in mylabel 1 if i sets to 2, the text goes down so i cant see.
now finally i want to display 2lines of Title, 1 line of pubDate and 3 Lines of Description in one row.
i have displayed but i need some alignment i.e. above points, it should remove HTML(&mdash) tags
i dont know hw to customizing this.struggling with this.pls help me out
Subclass the UITableViewCell and use an that custom cell.
First problem here is that you add the labels each time your cell is displayed. But you may reuse some cells that all ready have in their contentView the labels so you can reuse these labels.
Second problem is the performance each time you alloc init and release 3 labels on each row is displayed. This will produce slow scroll speed on slow devices like iPhone 3G.
Take a look at apple's CustomTableViewCell sample code.
Here is an tutorial how to subClass UITableViewCell
With an custom cell class your method will Look like this
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setLabel1String:aBook.title];
[cell setLabel2String:aBook.pubDate];
[cell setLabel3String:aBook.description];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
I have a TableView in which I am adding custom UILabels to the UITableViewCells. The tableView loads fine, but when it tries to dequeue a cell after scrolling the app crashes when it tries to set the text of the UILables. Code follows:
#define STYLE_NUMBER_TAG 0
#define COLORWAY_TAG 1
#define SIZE_TAG 2
#define QUANTITY_TAG 3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the managedObject
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
OrderLineItem *item = (OrderLineItem *)managedObject;
static NSString *CellIdentifier = #"lineItemCell";
UILabel *styleNumberLabel, *colorwayLabel, *sizeLabel, *quantityLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
quantityLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 70, 20)] autorelease];
quantityLabel.tag = QUANTITY_TAG;
[cell.contentView addSubview:quantityLabel];
styleNumberLabel = [[[UILabel alloc] initWithFrame:CGRectMake(85, 5, 70, 20)] autorelease];
styleNumberLabel.tag = STYLE_NUMBER_TAG;
[cell.contentView addSubview:styleNumberLabel];
colorwayLabel = [[[UILabel alloc] initWithFrame:CGRectMake(165, 5, 70, 20)] autorelease];
colorwayLabel.tag = COLORWAY_TAG;
[cell.contentView addSubview:colorwayLabel];
sizeLabel = [[[UILabel alloc] initWithFrame:CGRectMake(245, 5, 70, 20)] autorelease];
sizeLabel.tag = SIZE_TAG;
[cell.contentView addSubview:sizeLabel];
} else {
styleNumberLabel = (UILabel *)[cell.contentView viewWithTag:STYLE_NUMBER_TAG];
colorwayLabel = (UILabel *)[cell.contentView viewWithTag:COLORWAY_TAG];
sizeLabel = (UILabel *)[cell.contentView viewWithTag:SIZE_TAG];
quantityLabel = (UILabel *)[cell.contentView viewWithTag:QUANTITY_TAG];
}
// Configure the cell...
styleNumberLabel.text = item.style.styleNumber; //CRASHES HERE when dequeueing
colorwayLabel.text = item.colorway;
sizeLabel.text = item.size;
quantityLabel.text = [item.quantity stringValue];
return cell;
}
Thanks
There are two factors at play here:
The default value of a UIView's tag
is 0.
The receiver of viewWithTag: is
included in the search.
Consequently, your call to [cell.contentView viewWithTag:STYLE_NUMBER_TAG] is returning the content view itself when you really want it to return the UILabel.
The solution is simple. Don't use 0 as a tag.