Resetting a custom UITableViewCells content when its being reused - objective-c

I'm using the following code to create a custom UITableViewCell with a label in it.
The problem I have is that the cell doesn't get cleared when it's reused and I'm unsure of how to do so as I don't think I have access to the cell contents when it's dequeued.
Can anyone tell me how I'd do this please?
if (indexPath.section == 2) {
// Resetting the elements in the cell prior to use.
UIView *blankCell;
UILabel *methodText;
static NSString *methodCellIdentifier = #"Method Cell";
methodCell = [tableView dequeueReusableCellWithIdentifier: methodCellIdentifier];
if (methodCell == nil)
{
methodCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: methodCellIdentifier] autorelease];
}
blankCell = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
methodText = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 305, 44)];
methodText.text = [[self.recipeDict objectForKey:#"Method"] objectAtIndex: indexPath.row];
NSLog(#"methodText.text = %#", methodText.text);
[methodText setLineBreakMode:UILineBreakModeWordWrap];
[methodText setNumberOfLines:0];
[methodText sizeToFit];
[blankCell addSubview:methodText];
[methodCell.contentView addSubview:blankCell];
[methodText release];
methodCell.contentMode = UIViewContentModeRedraw;
return methodCell;
}

Move the code that creates your subviews inside the block that checks if methodCell is nil. Only update their content outside of that block. To access them outside the block, set tag values on them when creating them and use viewWithTag to find and update their content:
if (methodCell == nil)
{
methodCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: methodCellIdentifier] autorelease];
blankCell = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
methodText = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 305, 44)];
[methodText setLineBreakMode:UILineBreakModeWordWrap];
[methodText setNumberOfLines:0];
[blankCell addSubview:methodText];
[methodCell.contentView addSubview:blankCell];
methodText.tag = 1;
[methodText release];
[blankCell release];
methodCell.contentMode = UIViewContentModeRedraw;
}
methodText = (UITextField*)[methodCell viewWithTag:1];
methodText.text = [[self.recipeDict objectForKey:#"Method"] objectAtIndex: indexPath.row];
NSLog(#"methodText.text = %#", methodText.text);
[methodText sizeToFit];

Related

UITextView on cell clears when scrolling

Hello fellow programmers,
I am really cracking my head trying to understand what the problem is, but I just can't seem to figure out the problem. I have done research on this but nothing works for me unfortunately. The problem is that I have multiple cells and each cell is given a specific UITextField which I draw on the cellForRowAtIndexPath programmatically. The issues is that when I enter some information on that any Text Field and I scroll down on the table view, the text on the text field disappears into thin air. I am trying to understand why the cell reuse (which I believe is causing the problem), is causing this.
Here is my code for the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
cellText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 260, 20)];
cellText.font = [UIFont boldSystemFontOfSize:11.0];
cellText.textColor = [UIColor grayColor];
[cell.contentView addSubview:cellText];
if (indexPath.section == 0)
{
cellText.text = clientInfoArray[indexPath.row];
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Customer Name"])
{
nameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
nameTxtField.placeholder = #"full name";
nameTxtField.font = [UIFont boldSystemFontOfSize:15.0];
nameTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[nameTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:nameTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Phone"])
{
phoneTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
phoneTxtField.placeholder = #"xxx-xxx-xxx";
phoneTxtField.font = [UIFont boldSystemFontOfSize:15.0];
phoneTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
phoneTxtField.keyboardType = UIKeyboardTypePhonePad;
[phoneTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:phoneTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Date"])
{
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
dateLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:dateLabel];
dateLabel.text = [dateFormatter stringFromDate:[NSDate date]];
}
}
if (indexPath.section == 1) {
cellText.text = vehicleInfoArray[indexPath.row];
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Make and Model"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
modelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:modelLabel];
modelLabel.text = makeAndModelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Color"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
colorLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:colorLabel];
colorLabel.text = colorData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Doors"])
{
doorsTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
doorsTxtField.placeholder = #"#";
doorsTxtField.font = [UIFont boldSystemFontOfSize:15.0];
doorsTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[doorsTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
doorsTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:doorsTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Vehicle VIN"])
{
vinTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
vinTxtField.placeholder = #"#";
vinTxtField.font = [UIFont boldSystemFontOfSize:15.0];
vinTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[vinTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
vinTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:vinTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Fuel"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
fuelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
fuelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:fuelLabel];
fuelLabel.text = fuelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Milage"])
{
milageLabel = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
milageLabel.placeholder = #"ODO";
milageLabel.font = [UIFont boldSystemFontOfSize:15.0];
milageLabel.autocorrectionType = UITextAutocorrectionTypeNo;
[milageLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
milageLabel.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:milageLabel];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Other description"])
{
otherDTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, 300, 125)];
otherDTxtView.font = [UIFont boldSystemFontOfSize:15.0];
[cell addSubview:otherDTxtView];
}
}
if (indexPath.section == 2) {
cellText.text = InAndOutInfoArray[indexPath.row];
}
return cell;
}
Any ideas on how I can fix this problem? + Extra points for a helpful answer. :)
Whenever you are using reuse cell techniques, the cell that is not visible will be deallocated to free some memory.
Since you are creating a textfield in the cellForRow method, it will also be deallocated when the cell is not visible and redrawn when it becomes visible.
You could save the textfield value in some array (using textfield's didEndEditing delegate) and use that array to populate the text during textfield creation process inside the cellForRow method.

How to fix: initWithFrame:CellFrame reuseIdentifier:cellIdentifier deprecated

I upgraded to XCODE 4.2 and suddenly i got all these warning signals. Most of the i am able to fix but the following I do not know how to. I have tried to read-up on it but still have problem.
The code that is deprecated is:
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
I know that i need to use the following:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
However, i get problem with the CellFrame etc. when i test.
Could someone please give me a hint how i should replace the deprecated code with the initWithStyle and get the same result?
Here is the full code:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 10, 290, 25);
CGRect Label2Frame = CGRectMake(30, 33, 270, 25);
CGRect Label3Frame = CGRectMake(30, 56, 270, 25);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:16]];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Initialize Label with tag 2.
lblTemp = [[UILabel alloc] initWithFrame:Label2Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 2;
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Initialize Label with tag 3.
lblTemp = [[UILabel alloc] initWithFrame:Label3Frame];
lblTemp.backgroundColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.3f alpha:1.0f];
lblTemp.tag = 3;
[lblTemp setFont:[UIFont fontWithName:#"American Typewriter" size:13]];
lblTemp.textColor = [UIColor whiteColor];
lblTemp.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
return cell;
}
Just at first initialize with style, and after set reuired frame. I don't see any problems:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.frame = CellFrame;
}
Something along those lines
cell = [[[UITableViewCell alloc] initWithStyle:somestyle reuseIdentifier:#"cellname"] autorelease]
cell.frame = cellFrame;
Iam not sure if you really need to set the cell frame unless you want some specific frame, it should be set to whatever the tableview is.
i just use this code
cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}

how can i make a uitableview container to bounce within the view

ok, so this is pretty simple one, but i hope i could explain this clearly - i have a table view that i would like to inset into a container, and then have the table bounces when it reaches the top / bottom. So far, I was able to put my table in a container, but the container is fixed on the view, while the table inside the container bounces. Again, I am looking for a way to fix the table to the container, while having the container bouncing.
Here is what I was able to do, following the code:
What I want to accomplish is to have the black box bouncing rather than the table within it.
my ViewDidLoad in the view controller .m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//General View Setup
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"backgroundimage.png"]];
self.view.backgroundColor = background;
//Table View Data
listOfItems = [[NSMutableArray alloc] init];
NSArray *appleComputers = [NSArray arrayWithObjects:#"iPhone",#"iPod",#"MacBook",#"MacBook Pro",nil];
NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:#"Computers"];
NSArray *otherComputers = [NSArray arrayWithObjects:#"HP", #"Dell", #"Windows", #"Sony", #"Ivory", #"IBM", nil];
NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:#"Computers"];
[listOfItems addObject:appleComputersDict];
[listOfItems addObject:otherComputersDict];
self.navigationItem.title = #"Computers";
// Create a table
tblSimpleTable.delegate = self;
CGRect cgRct = CGRectMake(10, 50, 300, 300);
tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStyleGrouped]; // Initilize the table
[tblSimpleTable setBackgroundColor:[UIColor blackColor]];
tblSimpleTable.sectionHeaderHeight = 30.0;
tblSimpleTable.sectionFooterHeight = 30.0;
tblSimpleTable.delegate = self;
tblSimpleTable.dataSource = self;
[self.view addSubview:tblSimpleTable];
//Create the header
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor yellowColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
self.tblSimpleTable.tableHeaderView = containerView;
}
why don’t you use UIScrollView for that.
I had tested your code & done required changes. Hope you like it.
Code :
(this is your .h file)
#import <UIKit/UIKit.h>
#interface tableScrollViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource, UIScrollViewDelegate> {
UITableView *tblSimpleTable;
NSMutableArray *listOfItems;
NSMutableArray *appleComputers,*otherComputers;
UIScrollView *scrollView;
}
#end
(this is your .m file)
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor grayColor];
scrollView.contentSize = CGSizeMake(300, 800);
appleComputers = [[NSMutableArray alloc] init]; // I made it by my style
[appleComputers addObject: #"iPhone"];
[appleComputers addObject:#"iPod"];
[appleComputers addObject:#"MacBook"];
[appleComputers addObject:#"MacBook Pro"];
otherComputers = [[NSMutableArray alloc] init];
[otherComputers addObject: #"HP"];
[otherComputers addObject:#"Dell"];
[otherComputers addObject:#"Windows"];
[otherComputers addObject:#"Sony"];
[otherComputers addObject:#"Ivory"];
[otherComputers addObject:#"IBM"];
self.navigationItem.title = #"Computers";
// Create a table
tblSimpleTable.delegate = self;
CGRect cgRct = CGRectMake(10, 50, 300, 600);
tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct
style:UITableViewStyleGrouped]; // Initilize the table
[tblSimpleTable setBackgroundColor:[UIColor blackColor]];
tblSimpleTable.sectionHeaderHeight = 30.0;
tblSimpleTable.sectionFooterHeight = 30.0;
tblSimpleTable.scrollEnabled = NO;
tblSimpleTable.delegate = self;
tblSimpleTable.dataSource = self;
[scrollView addSubview:tblSimpleTable];
self.view = scrollView;
//Create the header
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(#"Header for the table", #"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor yellowColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
tblSimpleTable.tableHeaderView = containerView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return [appleComputers count];
else if(section == 1)
return [otherComputers count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0)
cell.text = [NSString stringWithFormat:#"%#“,
[appleComputers objectAtIndex:indexPath.row]];
else if(indexPath.section == 1)
cell.text = [NSString stringWithFormat:#"%#“,
[otherComputers objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath
{
// do whatever here
}

UITableviewcell loading data issue

I parsed the XML data from a feed. I stored all data in NSArray. I loaded the images and title in UITableview using UILable. But when I use the scroll, the text will be collapsed as follows.
My code is as follows...
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:13.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.text=[title1 objectAtIndex:indexPath.row];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[cell.contentView addSubview:valueField];
[valueField release];
UIImage *patternImage = [UIImage imageNamed:#"bg.jpg"];
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor colorWithPatternImage: patternImage];
cell.backgroundView = backView;
NSString *image1 =[images objectAtIndex:indexPath.row];
NSLog(#"Images ..... = %#",image1);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:indexPath.row]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: myimage ];
imageView.frame = CGRectMake(0.0f, 00.0f, 60.0f, 63.0f);
[cell.contentView addSubview:imageView];
//cell.imageView.image = myimage;
return cell;
}
I don't know how to solve this issue.
Every time the cell is reloaded, the UILabel is added.
Do something like
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:#"Helvetica" size:13.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[cell.contentView addSubview:valueField];
[valueField release];
}
valueField.text=[title1 objectAtIndex:indexPath.row];
To be more specific, since you are dequeuing cells, when you call cellForRowAtIndexPath it potentially can return you a cell that was already created.
That cell already has a label created.
So all of the magic of creating and setting up the custom views in you cell should only be done when dequeueReusableCellWithIdentifier returns a nil value

Why the dummy text can't load on my cell?

This is the tableView, as you can see, which cell, have two part, the left one, is a leftUIView, and right one, is the rightUIView. The red and green color can display, but the rightLabel I create can't show successfully. What's wrong with my code? Thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SectionsTableIdentifier] autorelease];
}
cell.textLabel.textColor = [UIColor whiteColor];
UIView *rightUIView = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, cell.frame.size.height)];
[rightUIView setBackgroundColor:[UIColor greenColor]];
UIView *leftUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, cell.frame.size.height)];
[leftUIView setBackgroundColor:[UIColor redColor]];
UILabel *rightLabel = [[UILabel alloc] init];
[rightLabel setText:#"dummy"];
[rightUIView addSubview:rightLabel];
[cell addSubView:leftUIView];
[cell addSubView:rightUIView];
}
The first problem with your code is that you create cell's subviews each time cell is requested - if you scroll your table to and fro your cell will have a bunch of those subviews - certainly that's not good. You must create cell subviews only once - when you create the cell and after that only set appropriate values to them:
const int kRightViewTag = 100;
const int kRightLabelTag = 200;
...
if (cell == nil) {
// Create everything here
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SectionsTableIdentifier] autorelease];
cell.textLabel.textColor = [UIColor whiteColor];
UIView *rightUIView = [[UIView alloc] initWithFrame:CGRectMake(160, 0, 160, cell.frame.size.height)];
rightUIView.tag = kRightViewTag;
[rightUIView setBackgroundColor:[UIColor greenColor]];
[rightUIView release]; // Do not forget to release object you allocate!!
UIView *leftUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, cell.frame.size.height)];
[leftUIView setBackgroundColor:[UIColor redColor]];
[leftUIView release]; // Do not forget to release object you allocate!!
UILabel *rightLabel = [[UILabel alloc] initWithFrame:leftUIView.bounds];
rightLabel.tag = kRightLabelTag;
[rightUIView addSubview:rightLabel];
[rightLabel release];
}
// Setup values here
UILabel* rightLabel = (UILabel*)[[cell viewWithTag:kRightViewTag] viewWithTag:kRightLabelTag];
rightLabel.text = #"dummy";
The second thing is you do not set label's frame anywhere (I've fixed that above) so it is likely has zero-sized frame and that's why you cannot see it.
The 3rd problem is that you allocate views but don't release them - so they just leak. Do not forget that if you create some objects using alloc, new or copy then you're responsible for releasing them.