moving up problem in custom table view cell - iphone

I am using custom table view cell to show to table view
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
{
m_mail_status = [[UIImageView alloc] initWithFrame:CGRectMake(295, 10, 18, 18)];
[self addSubview:m_mail_status];
m_subject = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 270, 37)];
UIFont* font = m_subject.font;
m_subject.font = [UIFont boldSystemFontOfSize:font.pointSize];
//m_subject.font = [UIFont systemFontOfSize:17];
[self addSubview:m_subject];
m_from = [[UILabel alloc] initWithFrame:CGRectMake(10, 37, 200, 15)];
m_from.font = [UIFont systemFontOfSize:12];
[self addSubview:m_from];
m_date = [[UILabel alloc] initWithFrame:CGRectMake(180, 37, 140, 15)];
m_date.textAlignment= UITextAlignmentRight;
m_date.font = [UIFont systemFontOfSize:12];
[self addSubview:m_date];
}
return self;
}
and my code in view controller,
static NSString *MyIdentifier = #"ProductsCell";
InboxViewCell *cell = (InboxViewCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
//static NSString *CellIdentifier = #"Cell";
// UITableViewCell *cell = nil;// [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"xx"] autorelease];
if(cell == nil)
cell = [[[InboxViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.m_subject.text = [[inbox objectAtIndex:indexPath.row] objectForKey:#"subject"];
cell.m_from.text = [[inbox objectAtIndex:indexPath.row] objectForKey:#"from"];
cell.m_date.text = [[inbox objectAtIndex:indexPath.row] objectForKey:#"date"];
if([[[inbox objectAtIndex:indexPath.row] objectForKey:#"**reservation_status**"] isEqualToString:#"1"])
{
cell.m_mail_status.image = [UIImage imageNamed:#"reservation_symbol.png"];
}
It loads in proper manner when it first loads
The issue when i scroll down and move to up
all the image is shown it ignores the if statement result
Thanks in advance
Regards,
sathish

The reason for this is that the cells are reused, you will want to reset that image in the else of your if.

Related

UILabel overlapping in UITableViewCell

I have a 2x UILabels, 1 title 1 subtitle,
Which are meant to change when a segmentedControl is selected.
It works but instead i get the SAME UILabel overlapping itself when a different segment is selected?
I think i need to create an action to remove the label from the superview before it is redisplayed onto the cell? just wondering how to go about it
- (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];
}
EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section];
EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section];
UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)];
UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)];
NSString *titleString = [[NSString alloc] init];
NSString *subtitleString = [[NSString alloc] init];
if (segmentedControl.selectedSegmentIndex == 0)
{
Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row];
titleString = aEvent.name;
subtitleString = aEvent.subtitle;
}
else
{
Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row];
titleString = aEvent.name;
subtitleString = aEvent.subtitle;
}
NSString *titleStringUC = [titleString uppercaseString];
NSString *subtitleStringLC = [subtitleString lowercaseString];
cellTitle.text = titleStringUC;
cellTitle.font = [UIFont boldSystemFontOfSize:11];
cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1];
cellTitle.shadowColor = [UIColor whiteColor];
cellTitle.shadowOffset = CGSizeMake(1, 1);
cellTitle.backgroundColor = [UIColor clearColor];
cellSubtitle.text = subtitleStringLC;
cellSubtitle.font = [UIFont boldSystemFontOfSize:11];
cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1];
cellSubtitle.shadowColor = [UIColor whiteColor];
cellSubtitle.shadowOffset = CGSizeMake(1, 1);
cellSubtitle.backgroundColor = [UIColor clearColor];
tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TableCellSeparator.png"]];
tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.contentView addSubview:cellTitle];
[cell.contentView addSubview:cellSubtitle];
[cell.contentView addSubview:tableViewCellSeparator];
return cell;
}
UPDATE:
Both were very valid answers, tyvm
You're not reusing your cell correctly, so you're not getting the performance benefit of reuse, and making the code more complicated. You're also not using the pieces that Apple gives you to work with out of the box.
First, you should create and add all your subviews inside the cell==nil block. This is where you create your reusable cell. In the rest of the routine, you're just reconfiguring the cell. This is much, much faster.
Second, UITableViewCell already has two labels built-in. You don't need to create them. They're called textLabel and detailTextLabel. They're normal labels; you can move them around and make them look like whatever you want. Do that in the cell==nil block.
Then you just need to call cell.textLabel.text = ... and cell.detailTextLabel.text = ... outside the cell==nil block and you're good to go.
If you needed more labels than two, then I would subclass UITableViewCell and create new properties on it so that you can easily reconfigure the cell.
I hope it will work now.Try this.....
- (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];
EventUpcoming *aEventUpcoming = [euEvent objectAtIndex:indexPath.section];
EventWeekly *aEventWeekly = [ewEvent objectAtIndex:indexPath.section];
UILabel *cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 290, 20)];
UILabel *cellSubtitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, 290, 20)];
NSString *titleString = [[NSString alloc] init];
NSString *subtitleString = [[NSString alloc] init];
NSString *titleStringUC = [titleString uppercaseString];
NSString *subtitleStringLC = [subtitleString lowercaseString];
cellTitle.text = titleStringUC;
cellTitle.font = [UIFont boldSystemFontOfSize:11];
cellTitle.textColor = [UIColor colorWithRed:142/255.0f green:142/255.0f blue:142/255.0f alpha:1];
cellTitle.shadowColor = [UIColor whiteColor];
cellTitle.shadowOffset = CGSizeMake(1, 1);
cellTitle.backgroundColor = [UIColor clearColor];
cellTitle.tag = 10;
cellSubtitle.text = subtitleStringLC;
cellSubtitle.font = [UIFont boldSystemFontOfSize:11];
cellSubtitle.textColor = [UIColor colorWithRed:177/255.0f green:177/255.0f blue:177/255.0f alpha:1];
cellSubtitle.shadowColor = [UIColor whiteColor];
cellSubtitle.shadowOffset = CGSizeMake(1, 1);
cellSubtitle.backgroundColor = [UIColor clearColor];
cellSubtitle.tag = 11;
tableViewCellSeparator = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TableCellSeparator.png"]];
tableViewCellSeparator.frame = CGRectMake(0, cell.bounds.size.height - 2, 320, 2);
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.contentView addSubview:cellTitle];
[cell.contentView addSubview:cellSubtitle];
[cell.contentView addSubview:tableViewCellSeparator];
}
cellTitle = (UILabel *)[cell.contentView viewWithTag:10];
cellSubtitle = (UILabel *)[cell.contentView viewWithTag:11];
if (segmentedControl.selectedSegmentIndex == 0)
{
Event *aEvent = [aEventUpcoming.event objectAtIndex:indexPath.row];
titleString = aEvent.name;
subtitleString = aEvent.subtitle;
}
else
{
Event *aEvent = [aEventWeekly.event objectAtIndex:indexPath.row];
titleString = aEvent.name;
subtitleString = aEvent.subtitle;
}
return cell;
}

UITableview dequeueReusableCellWithIdentifier and scroll-freezing issues

So I have some issues with my tableview. I have a custom label that I put into a tableview cell to add a little better graphics than the standard UItableviewcell. However, I was running into my first problem,
the text labels that I had on the cells were changing with and over writing each other upon scrolling, only when the cells had moved off screen and then came back. Upon some research I found that maybe it had something to do with dequeueReusableCellWithIdentifier: so I adjusted my code. this is where problem two comes in.
When I load the table everything is in its right place, correct looking and all. However when I start to scroll down I can get to all of my cells except the last one, it will go to the very bottom of the 8th cell and freeze, but I should have 9 cells loaded.
I am quite confused by some of this, could anyone provide some code or guidance to help me along?
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Run");
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"Cell";
UILabel *label;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *keys = [[appDelegate rowersDataStore] allKeys];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, cell.bounds.size.width - 10, 30)] autorelease];
label.font = [UIFont boldSystemFontOfSize:16];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];
label.shadowOffset = CGSizeMake(0,1);
label.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0];
switch (indexPath.section) {
case 0:
label.frame = CGRectMake(0, 15, cell.bounds.size.width - 10, 30);
label.textAlignment = UITextAlignmentCenter;
break;
case 1:
label.textAlignment = UITextAlignmentLeft;
UIImage *accessoryImage = [UIImage imageNamed:#"content_arrow.png"];
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
cell.accessoryView = accessoryView;
[accessoryView release];
break;
}
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage* img = [UIImage imageNamed:#"odd_slice.png"];
imgView.image = img;
cell.backgroundView = imgView;
[imgView release];
//Selected State
UIImage *selectionBackground = [UIImage imageNamed:#"row_selected.png"];
UIImageView *selectionView = [[UIImageView alloc] initWithFrame:cell.frame];
selectionView.image = selectionBackground;
cell.selectedBackgroundView = selectionView;
[selectionView release];
}
switch (indexPath.section) {
case 0:
[label setText:#"Click to add new rower"];
break;
case 1:
[label setText:[[[appDelegate rowersDataStore] objectForKey:[keys objectAtIndex:indexPath.row]] objectForKey:#"Name"]];
break;
}
//Adds Text
[cell addSubview:label];
return cell;
}
I see several issues here. First, the general structure of this method should be...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
// Attempt to dequeue the cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// If cell does not exist, create it, otherwise customize existing cell for this row
if (cell == nil) {
// Create cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure cell:
// *** This section should configure the cell to a state independent of
// whatever row or section the cell is in, since it is only executed
// once when the cell is first created.
}
// Customize cell:
// *** This section should customize the cell depending on what row or section
// is passed in indexPath, since this is executed every time this delegate method
// is called.
return cell;
}
Basically, UITableView uses a single UITableViewCell instance to draw every cell in the table view. So, when you first create this cell, you should configure it to a state that is common to all cells that will use this instance, independent of whatever row or section is passed in indexPath. In your example, this involves creating the label, image, and background image instances and adding them as subviews to the cell.
Once the cell is created (aka outside the if (cell == nil) statement), you should customize its properties according to how the cell should look for the specific row and section contained in indexPath. Since you want to access your custom label in this part of the code, I assigned a tag value to it so that we can access it beyond the code segment where it was created using viewWithTag:. Once we have the label, we can customize it according to the section as well as do anything else we want, such as customize the accessory view.
I slightly modified/cleaned up your code below. This is by far not the most efficient or elegant way to do what you want to do, but I was trying to keep as much of your code as possible. I haven't tested this, but if you try it it should work:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"Run");
CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *keys = [[appDelegate rowersDataStore] allKeys];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
UILabel *label;
label = [[[UILabel alloc] initWithFrame:CGRectMake(20, 15, cell.bounds.size.width - 10, 30)] autorelease];
label.font = [UIFont boldSystemFontOfSize:16];
label.opaque = NO;
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.5];
label.shadowOffset = CGSizeMake(0,1);
label.textColor = [UIColor colorWithRed:0x4c/255.0 green:0x4e/255.0 blue:0x48/255.0 alpha:1.0];
label.tag = 100;
[cell addSubview:label];
[label release];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.frame];
UIImage* img = [UIImage imageNamed:#"odd_slice.png"];
imgView.image = img;
cell.backgroundView = imgView;
[imgView release];
//Selected State
UIImage *selectionBackground = [UIImage imageNamed:#"row_selected.png"];
UIImageView *selectionView = [[UIImageView alloc] initWithFrame:cell.frame];
selectionView.image = selectionBackground;
cell.selectedBackgroundView = selectionView;
[selectionView release];
}
UILabel *lbl = (UILabel *)[cell viewWithTag:100];
switch (indexPath.section) {
case 0:
cell.accessoryView = nil;
lbl.frame = CGRectMake(0, 15, cell.bounds.size.width - 10, 30);
lbl.textAlignment = UITextAlignmentCenter;
[label setText:#"Click to add new rower"];
break;
case 1:
UIImage *accessoryImage = [UIImage imageNamed:#"content_arrow.png"];
UIImageView *accessoryView = [[UIImageView alloc] initWithImage:accessoryImage];
cell.accessoryView = accessoryView;
[accessoryView release];
lbl.frame = CGRectMake(20, 15, cell.bounds.size.width - 10, 30);
lbl.textAlignment = UITextAlignmentLeft;
[lbl setText:[[[appDelegate rowersDataStore] objectForKey:[keys objectAtIndex:indexPath.row]] objectForKey:#"Name"]];
break;
}
return cell;
}

'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'

my code is
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
/// labels - names of Cities ///
UILabel *lblCity = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 22)];
lblCity.font = [UIFont systemFontOfSize:14];
lblCity.backgroundColor = [UIColor clearColor];
//lblCity.backgroundColor = [UIColor redColor];
UILabel *lblDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 22)];
lblDate.font = [UIFont systemFontOfSize:14];
lblDate.backgroundColor = [UIColor clearColor];
//lblDate.backgroundColor = [UIColor redColor];
UILabel *lblSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 22)];
lblSchool.font = [UIFont systemFontOfSize:14];
lblSchool.backgroundColor = [UIColor clearColor];
//lblSchool.backgroundColor = [UIColor redColor];
/// Labels for description of city events ///
UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 200, 30)];
lblEvent.font = [UIFont systemFontOfSize:12];
lblEvent.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 200, 30)];
lblEventAtDate.font = [UIFont systemFontOfSize:12];
lblEventAtDate.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 00, 400, 30)];
lblEventAtSchool.font = [UIFont systemFontOfSize:12];
lblEventAtSchool.backgroundColor = [UIColor clearColor];
if(RequestType == 2)
{
UIImageView *imgEventLabel = [[UIImageView alloc]initWithFrame:CGRectMake(00, 00, 480, 22)];
UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];
if(indexPath.row == 0)
{
static NSString *CellIdentifier = #"Cell11";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
lblCity.text = #"City" ;
// [cell addSubview:lblCity];
lblDate.text = #"Date" ;
// [cell addSubview:lblDate];
lblSchool.text = #"School" ;
// [cell addSubview:lblSchool];
imgEventLabel.image = [UIImage imageNamed:#"city_date_place.png"];
// [cell addSubview:imgEventLabel];
[imgEventLabel addSubview:lblCity];
[imgEventLabel addSubview:lblDate];
[imgEventLabel addSubview:lblSchool];
[cell.contentView addSubview:imgEventLabel];
}
return cell;
}
if(indexPath.row == 1)
{
static NSString *CellIdentifier = #"Cell12";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines = 999;
lblEvent.text = #"Event in City";
lblEventAtDate.text = #"Event on Date";
lblEventAtSchool.text = #"Event at School";
[viewDescription addSubview:lblEvent];
[viewDescription addSubview:lblEventAtDate];
[viewDescription addSubview:lblEventAtSchool];
[cell.contentView addSubview:viewDescription];
}
return cell;
}
}
// Configure the cell...
return cell;
}
I don't know where is fault, Please Help.
After your first call of dequeueReusableCellWithIdentifier: You didn't check for nil and create a new one if there are no available cells in the reuse queue.
In other words, if there are no cells in the reuse queue, your request type is not equal to 2, and row is not equal to 0 or 1, your cell will not be created. This causes an exception you see in the console.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
you don't need to write that on start as you create the cell according to conditions .
At the end just return nil. Because I think there must be some condition true when you execute your code
Sorry I do not have enough points to comment on your question. Have you tried using Custom Cells? Custom Cell is like a modification of the current TableViewCells. You can do almost anything you want to that the TableViewCells do not provide. From what I see, you want 3 Labels and 1 Image. A normal TableViewCell will not give you that ability to have 3 Labels. Unless you create it yourself. Below is a link for you to do so. I hope this is what you have in mind.
iPhone Custom Cell Tutorial

UITable view overlapping

- (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

UITableViewController crashes in tableView: cellForRowAtIndexPath:

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.