EXC_BAD_ACCESS when I scroll my view - objective-c

I have two views: the first one is a UITableView in plain style (created programmatically); the second one is a UIScrollView that contains an image and a UITableView in grouped style (also created programmatically).
When I navigate from the firstView -> secondView everything works fine. However if I come back to the firstView and then I try to navigate firstView ->secondView for the second time, I get a EXC_BAD_ACCESS error.
UPDATE
This is the code of the UITableView delegate in the secondView:
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
//header - breve descrizione
return 2;
break;
case 1: //mappa
//header - mappa statica (immagine) - footer (indirizzo)
return 3;
break;
case 2: //review
//header - prima review
return 2;
break;
default:
return 0;
break;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0: //case titolo (header)
return HTABLE_DESCR_HEADER;
break;
case 1: //case corpo
return HTABLE_DESCR_BODY;
break;
default:
return 0;
break;
}
break;
case 1:
switch (indexPath.row) {
case 0: //case titolo (header)
return HTABLE_LOC_HEADER;
break;
case 1: //case corpo
return HTABLE_LOC_BODY;
break;
case 2: //case footer
return HTABLE_LOC_FOOTER;
break;
default:
return 0;
break;
}
break;
case 2:
switch (indexPath.row) {
case 0: //case titolo (header)
return HTABLE_REV_HEADER;
break;
case 1: //case corpo
return HTABLE_REV_BODY;
break;
default:
return 0;
break;
}
break;
default:
return 0;
break;
}
}
/*- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"Travellers Guide";
}*/
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
int section = 2;
//se ci sono review
if ([[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] count] > 0) {
section++;
}
return section;
}
// 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 = [self getCellContentView:CellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
switch (indexPath.section) {
case 0: //descrizione
switch (indexPath.row) {
case 0: //header
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"language"] isEqualToString:#"it"]) {
cell.textLabel.text = #"Descrizione";
} else {
cell.textLabel.text = #"Description";
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaDescrHeader.png"]];
break;
case 1: //descrizione
NSLog(#"Descr");
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaRevBody.png"]];
cell.textLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"description"];
cell.textLabel.font = [UIFont systemFontOfSize:10.0];
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 3;
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset =CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
break;
default:
break;
}
break;
case 1: //mappa
//header - mappa statica (immagine) - footer (indirizzo)
switch (indexPath.row) {
case 0: //header
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"language"] isEqualToString:#"it"]) {
cell.textLabel.text = #"Posizione";
} else {
cell.textLabel.text = #"Location";
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaLocHeader.png"]];
break;
case 1: //mappa
NSLog(#"Mappa");
CLLocationDegrees latitude = [[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"latitude"] doubleValue];
CLLocationDegrees longitude = [[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"longitude"] doubleValue];
CLLocation* poiLocation = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
Annotation *ann = [Annotation annotationWithCoordinate:poiLocation.coordinate];
//mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, HTABLE_LOC_BODY)];
[mapView setHidden:NO];
[mapView addAnnotation:ann];
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = poiLocation.coordinate;
region.span.longitudeDelta = 0.05f;
region.span.latitudeDelta = 0.05f;
[self.mapView setRegion:region animated:YES];
[self.mapView regionThatFits:region];
cell.backgroundView = mapView;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 302, 120)];
imgView.image = [UIImage imageNamed:#"ombraMappa.png"];
[cell.backgroundView addSubview:imgView];
[imgView release];
break;
case 2: //footer
cell.textLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"locality"];
cell.textLabel.font =[UIFont systemFontOfSize:10.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.detailTextLabel.text = [[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"address"];
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:9.0];
cell.detailTextLabel.textColor = [UIColor grayColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.shadowColor = [UIColor whiteColor];
cell.detailTextLabel.shadowOffset = CGSizeMake(1, 1);
cell.detailTextLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaLocFooter.png"]];
break;
default:
break;
}
break;
case 2: //review
//header - mappa statica (immagine) - footer (indirizzo)
switch (indexPath.row) {
case 0: //header
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"language"] isEqualToString:#"it"]) {
cell.textLabel.text = [NSString stringWithFormat:#"Recensioni (%d)",[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] count]];
} else {
cell.textLabel.text = [NSString stringWithFormat:#"Review (%d)",[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] count]];
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:11.0];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaLocHeader.png"]];
break;
case 1: //review
cell.textLabel.text = [[[[[SingletonCardPOI sharedCardPOI] dicCard] objectForKey:#"list_review"] objectAtIndex:0] objectForKey:#"review"];
cell.textLabel.font =[UIFont systemFontOfSize:10.0];
cell.textLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.numberOfLines = 3;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.shadowColor = [UIColor whiteColor];
cell.textLabel.shadowOffset = CGSizeMake(1, 1);
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rigaRevBody.png"]];
break;
default:
break;
}
break;
default:
break;
}
return cell;
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
return cell;
}

The EXC_BAD_ACCESS isn't caused by the progression of one view to the next. Rather, it will be properties of those views, or the creation and release - or not - of the view objects themselves that are to blame.
Your first step is to run the Analyzer. Then, if you've fixed all that and theres still a problem, start running the Leaks tool in Instruments.
For more details on resolving EXC_BAD_ACCESS issues, along with the excellent link on what causes these errors and step by step instructions for how to fix, have a look at answers to these questions:
finding reason for EXC_BAD_ACCESS - in Xcode4
Random EXC_BAD_ACCESS in a place that it cannot happen
In your case, I'd specifically look at how you are creating secondView, and what you do with firstView when you return to secondView.

I just spent a week tracking down a EXC_BAD_ACCESS problem in a large app that worked fine before converting it to ARC. I would get the EXC_BAD_ACCESS on the closing bracket of a "switch" construct. Yet NSZombieEnabled and the various Instrument settings would not detect anything for me.
What fixed it was putting curlies around the case's that made the difference. I did it for each of them, though perhaps it was critical for only some, i.e.
Before:
case ...:
stmt;
stmt;
break;
After:
case ...:{
stmt;
stmt;
}break;
This fixed the problem in two places within a large app for me. I know that I read something somewhere about ARC and switch/case (and maybe someone can add a link to it), but I don't understand the theory behind why this happened and why this fix worked. Perhaps someone could explain. (Later: after reading the following reference at openradar, it all makes sense --- there is something wrong in the "cleanup code" generated between case statements --- a bogus "release" is generated and that soon causes the EXC_BAD_ACCESS.)
I also discovered this, which might be related: http://openradar.appspot.com/11329693

Related

How do I get the default CGRECT dimensions of uitableviewcell's detailtextlabel?

We're hoping to add a UIextLabel that replaces the default detailTextLabel of UITableViewCell's UITableViewCellStyleSubtitle. I can get the UITextLabel to show however the problem is that I can't get it aligned as if it were a detailTextLabel. The following code I've tried is this:
switch (indexPath.row) {
case 1:{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.numberOfLines = 1;
//cell.detailTextLabel.text = #"Enter your display name";
//self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
self.displayNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(cell.detailTextLabel.frame.origin.x, cell.detailTextLabel.frame.origin.y, cell.detailTextLabel.frame.size.height, cell.detailTextLabel.frame.size.width)];
self.displayNameTextField.placeholder = #"Enter your display name";
self.displayNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[self.displayNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:self.displayNameTextField];
break;
}
default:
break;
}
A small correction in your code. You can use your own code withe some changes.
Try this. It will work.
switch (indexPath.row){
case 1:{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.numberOfLines = 1;
UITextField *displayNameTextField = [[UITextField alloc] initWithFrame:cell.detailTextLabel.frame];
displayNameTextField.placeholder = #"Enter your display name";
displayNameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[displayNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:displayNameTextField];
break;
}
default:
break;
}

UITableViewCell with alternate background color in customized cells

I'd like the background to of my UITableViewCells to have a different color every two cells displayed, but when I scroll down and back, they all get the same color. How can I get this effect knowing that my cells have different contentView size (according to their content) ?
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 20.0f
#define NAME_CELL_HEIGHT 20.0f
#import "CartCell.h"
#implementation CartCell
#synthesize nameLabel = _nameLabel;
#synthesize ingredientsLabel = _ingredientsLabel;
#synthesize myStore;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
myStore = [Store sharedStore];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.nameLabel = nil;
self.ingredientsLabel = nil;
// SET "NAME" CELL
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.nameLabel setLineBreakMode:UILineBreakModeWordWrap];
[self.nameLabel setMinimumFontSize:FONT_SIZE];
[self.nameLabel setNumberOfLines:1];
[self.nameLabel setTag:1];
self.nameLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:18];
[self.nameLabel sizeToFit];
self.nameLabel.backgroundColor = [UIColor clearColor];
[[self contentView] addSubview:self.nameLabel];
// SET "INGREDIENTS" CELL
self.ingredientsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.ingredientsLabel setLineBreakMode:UILineBreakModeWordWrap];
[self.ingredientsLabel setMinimumFontSize:FONT_SIZE];
[self.ingredientsLabel setNumberOfLines:0];
[self.ingredientsLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[self.ingredientsLabel setTag:2];
self.ingredientsLabel.backgroundColor = [UIColor clearColor];
[[self contentView] addSubview:self.ingredientsLabel];
if (myStore.cellBackgroundShouldBeLight == YES) {
NSLog(#"clear [in] ? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
self.contentView.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1];
myStore.cellBackgroundShouldBeLight = NO;
} else {
NSLog(#"clear [in] ? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
self.contentView.backgroundColor = [[UIColor alloc]initWithRed:187.0/255.0 green:268.0/255.0 blue:229.0/255.0 alpha:1];
myStore.cellBackgroundShouldBeLight = YES;
}
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
UPDATE:
I'm know trying to set it in cellForRowAtIndexPath as it was suggested, but I get the same result: scrolling down worked fine the first time, but then scrolling up again messed up the cells background color.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CartCell";
CartCell *cell = (CartCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Recipes *info = [_fetchedResultsController objectAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[CartCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// if (!cell.nameLabel) {
// cell.nameLabel = (UILabel*)[cell viewWithTag:1];
// // cell.nameLabel = (UILabel*)[cell viewWithTag:1];
// }
// if (!cell.ingredientsLabel)
// cell.ingredientsLabel = (UILabel*)[cell viewWithTag:2];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [info.ingredients sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
[cell.nameLabel setFrame:CGRectMake(10, 10, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), NAME_CELL_HEIGHT)];
[cell.ingredientsLabel setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN + NAME_CELL_HEIGHT, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
// SETTING TEXT CONTENT
cell.nameLabel.text = info.name;
cell.ingredientsLabel.text = info.ingredients;
// SETTING BACKGROUND COLOR
// UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
// [lab setBackgroundColor:[UIColor blueColor]];
if (myStore.cellBackgroundShouldBeLight == YES) {
NSLog(#"clear? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
cell.contentView.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:84.0/255.0 blue:229.0/255.0 alpha:1];
// cell.backgroundView = lab;
// ingredientsLabel.backgroundColor = [UIColor clearColor];
// nameLabel.backgroundColor = [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1];
// [cell setBackgroundColor: [[UIColor alloc]initWithRed:87.0/255.0 green:168.0/255.0 blue:229.0/255.0 alpha:1]];
// [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
myStore.cellBackgroundShouldBeLight = NO;
} else {
// cell.contentView.tag = 2;
NSLog(#"clear? %#", myStore.cellBackgroundShouldBeLight ? #"Yes" : #"No");
cell.contentView.backgroundColor = [[UIColor alloc]initWithRed:187.0/255.0 green:184.0/255.0 blue:229.0/255.0 alpha:1];
myStore.cellBackgroundShouldBeLight = YES;
}
return cell;
}
It is very simple, the indexPath tells you everything you need to know. If the indexPath.row is even then use one color. If the indexPath.row is odd use a different color.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
…
// SETTING BACKGROUND COLOR
// UIView *lab = [[UIView alloc] initWithFrame:cell.frame];
// [lab setBackgroundColor:[UIColor blueColor]];
if (indexPath.row % 2) {
cell.contentView.backgroundColor = [[[UIColor alloc]initWithRed:87.0/255.0 green:84.0/255.0 blue:229.0/255.0 alpha:1] autorelease];
} else {
cell.contentView.backgroundColor = [[[UIColor alloc]initWithRed:187.0/255.0 green:184.0/255.0 blue:229.0/255.0 alpha:1] autorelease];
}
…
return cell;
}
Your method is having problems because blindly assuming cells will be asked for in alternating pairs is a bad assumption. The tableView could ask for cells in any order is chooses. In your example, I believe cells could be asked for as follows. First, 0, 1,…, 9 are asked for. Next, you scroll down and 10, 11, and 12 are fetched. At this point, 0, 1, and 2 have gone off the screen. You scroll back up and 2 is asked for, but oh no, your model is on an odd number alternation, so you get the wrong color.
Use the -willDisplayCell method.
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.row %2) { //change the "%2" depending on how many cells you want alternating.
UIColor *altCellColor = [UIColor colorWithRed:255/255.0 green:237/255.0 blue:227/255.0 alpha:1.0]; //this can be changed, at the moment it sets the background color to red.
cell.backgroundColor = altCellColor;
}
else if (indexPath.row %2) {
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0]; //this can be changed, at the moment it sets the background color to white.
cell.backgroundColor = altCellColor2;
}
}
The appropriate place to change your cell's background color would be the "cellForRowAtIndexPath:" method, where the cells data gets filled out and returned to the table view.
One way to do this would be: When the data goes into the cell, change the background color depending on what row you're on.
Put the color on the cellForRowAtIndexPath: don't set on custom cell.
Take a look what I use to customize my table
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#if USE_CUSTOM_DRAWING
const NSInteger TOP_LABEL_TAG = 1001;
const NSInteger BOTTOM_LABEL_TAG = 1002;
UILabel *topLabel;
UILabel *bottomLabel;
#endif
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//
// Create the cell.
//
cell =
[[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
autorelease];
#if USE_CUSTOM_DRAWING
UIImage *indicatorImage = [UIImage imageNamed:#"indicator.png"];
cell.accessoryView =
[[[UIImageView alloc]
initWithImage:indicatorImage]
autorelease];
const CGFloat LABEL_HEIGHT = 20;
UIImage *image = [UIImage imageNamed:#"imageA.png"];
//
// Create the label for the top row of text
//
topLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT),
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:topLabel];
//
// Configure the properties for the text that are the same on every row
//
topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
topLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
//
// Create the label for the top row of text
//
bottomLabel =
[[[UILabel alloc]
initWithFrame:
CGRectMake(
image.size.width + 2.0 * cell.indentationWidth,
0.5 * (aTableView.rowHeight - 2 * LABEL_HEIGHT) + LABEL_HEIGHT,
aTableView.bounds.size.width -
image.size.width - 4.0 * cell.indentationWidth
- indicatorImage.size.width,
LABEL_HEIGHT)]
autorelease];
[cell.contentView addSubview:bottomLabel];
//
// Configure the properties for the text that are the same on every row
//
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
bottomLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize] - 2];
//
// Create a background image view.
//
cell.backgroundView =
[[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView =
[[[UIImageView alloc] init] autorelease];
#endif
}
#if USE_CUSTOM_DRAWING
else
{
for (UIView *sub in [cell.contentView subviews]) {
// if([sub class] == [UITableViewCellContentView class])
NSLog(#"this is uilabel %#",[sub class]);
}
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
}
topLabel.text = [NSString stringWithFormat:#"Cell at row %ld.", [indexPath row]];
bottomLabel.text = [NSString stringWithFormat:#"Some other information.", [indexPath row]];
//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
UIImage *selectionBackground;
NSInteger sectionRows = [aTableView numberOfRowsInSection:[indexPath section]];
NSInteger row = [indexPath row];
if (row == 0 && row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:#"topAndBottomRow.png"];
selectionBackground = [UIImage imageNamed:#"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
rowBackground = [UIImage imageNamed:#"topRow.png"];
selectionBackground = [UIImage imageNamed:#"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
rowBackground = [UIImage imageNamed:#"bottomRow.png"];
selectionBackground = [UIImage imageNamed:#"bottomRowSelected.png"];
}
else
{
rowBackground = [UIImage imageNamed:#"middleRow.png"];
selectionBackground = [UIImage imageNamed:#"middleRowSelected.png"];
}
((UIImageView *)cell.backgroundView).image = rowBackground;
((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
// cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:rowBackground];
// cell.selectedBackgroundView.backgroundColor = [UIColor colorWithPatternImage:selectionBackground];
//
// Here I set an image based on the row. This is just to have something
// colorful to show on each row.
//
if ((row % 3) == 0)
{
cell.imageView.image = [UIImage imageNamed:#"imageA.png"];
}
else if ((row % 3) == 1)
{
cell.imageView.image = [UIImage imageNamed:#"imageB.png"];
}
else
{
cell.imageView.image = [UIImage imageNamed:#"imageC.png"];
}
#else
cell.text = [NSString stringWithFormat:#"Cell at row %ld.", [indexPath row]];
#endif
return cell;
}
past it after all #import lines
#define USE_CUSTOM_DRAWING 1
Heading ##Simplest way of changing alternate colors
if(indexPath.row%2) {
cell.backgroundColor=[UIColor nameUrColor] //brownColor, yellowColor, blueColor
} else {
cell.backgroundColor=[UIColor nameAnotherColor]
}
if(cell.contentView)
{
[cell.nameLbl setFont:[UIFont systemFontOfSize:24]];
int red_value = arc4random() % 210;
int green_value = arc4random() % 210;
int blue_value = arc4random() % 210;
cell.contentView.backgroundColor = [UIColor colorWithRed:red_value/255.0 green:green_value/255.0 blue:blue_value/255.0 alpha:0.6];
}

UITableView section not reloading when called

I want a section of my table view to reload whenever the ViewWillAppear method is called, I've implemented this like so:
- (void)viewWillAppear:(BOOL)animated {
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:0 inSection:1];
reloadRows = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
}
Here is the rowforsection method that indicates which content should appear in each tableview section:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"fadk");
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"PINGAS"];
[self.tableView setAlwaysBounceVertical:YES];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:#"PINGAS"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
// if ([indexPath section] == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 300, 41)];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, 300, 120)];
UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 0)] autorelease];
paddingView.backgroundColor = [UIColor cyanColor];
// if ([indexPath row] == 0) {
if ([indexPath section] == 0) {
NSLog(#"0");
[cell addSubview:textField];
if ([indexPath row] == 0) {
textField.placeholder = #"Title";
}
else{
textField.placeholder = #"Location";
}
}
else if ([indexPath section] == 1) {
NSLog(#"1");
NSDateFormatter *formatter;
NSString *eSString1;
NSString *eEString2;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"];
cell.textLabel.text = #"Starts\nEnds";
cell.textLabel.numberOfLines = 2;
eSString1 = [formatter stringFromDate:eSTime];
eEString2 = [formatter stringFromDate:eEtime];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#\n%#", eSString1, eEString2];
cell.detailTextLabel.numberOfLines = 2;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
NSLog(#"2");
[cell addSubview:textView];
}
textField.delegate = self;
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.adjustsFontSizeToFitWidth = YES;
textField.textColor = [UIColor blackColor];
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textField.textAlignment = UITextAlignmentLeft;
textField.tag = 0;
//playerTextField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[textField setEnabled: YES];
[textField release];
textView.delegate = self;
textView.textColor = [UIColor blackColor];
textView.keyboardType = UIKeyboardTypeAlphabet;
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor = [UIColor clearColor];
textView.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textView.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textView.textAlignment = UITextAlignmentLeft;
textView.tag = 0;
[textView release];
// }
}
return cell;
}
This works swimmingly the first load, and I after the first calling of viewWillAppear, but after that the section seems to recycle the data from the first load and the second load, and while it still enters the cellforrow section, it no longer goes into the section I call in the viewWIllAppear section.
The reload should be sandwiched between begin / end updates:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];

Remove top shadow in my grouped UITableView?

I am a little OCD and this is driving me insane. I have been messing around with these settings for a long time.
I have a UITableView grouped that I have a shadow on the top. When you tap the top cell, it removes. What gives?
I've been stressing over this for the past hour or so. Is there a simple solution for this? Or am I just going insane?
Thanks,
Coulton
EDIT:
viewDidLoad:
formTableView.backgroundColor = [UIColor clearColor];
formTableView.layer.borderColor = [UIColor clearColor].CGColor;
formTableView.separatorColor = [UIColor colorWithRed:(194.0 / 255.0) green:(194.0 / 255.0) blue:(194.0 / 255.0) alpha: 1];
Here is how I display my cells. WARNING: It's a lot of code. There's a bunch of stuff in there you will have to sort through, so sort through it at your own risk! :)
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
// What to do when you click delete.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
//RootViewController.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return [formDataOne count];
} else {
return [formDataTwo count];
}
}
//RootViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
// Set up the cell...
NSString *cellValue;
if (indexPath.section == 0) {
cellValue = [formDataOne objectAtIndex:indexPath.row];
} else {
cellValue = [formDataTwo objectAtIndex:indexPath.row];
}
if (indexPath.section == 0) {
cell.text = #"";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == 0) {
addTitle = [[UITextField alloc] initWithFrame:CGRectMake(13, 13, 280, 20)];
addTitle.borderStyle = UITextBorderStyleNone;
addTitle.textColor = [UIColor blackColor]; //text color
addTitle.font = [UIFont systemFontOfSize:16.0]; //font size
addTitle.placeholder = #"Album Name"; //place holder
addTitle.backgroundColor = [UIColor clearColor]; //background color
addTitle.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
addTitle.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
addTitle.returnKeyType = UIReturnKeyDone; // type of the return key
addTitle.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
addTitle.delegate = self; // let us be the delegate so we know when the keyboard's "Done" button is pressed
[cell.contentView addSubview:addTitle];
} else if (indexPath.row == 1) {
// Set up loading text and show it
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 13, 280, 20)];
myLabel.text = #"Private Album";
myLabel.textColor = [UIColor blackColor];
myLabel.textAlignment = UITextAlignmentLeft;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.font = [UIFont fontWithName:#"Helvetica" size: 16.0];
myLabel.numberOfLines = 0;
//[myLabel sizeToFit];
privateSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[privateSwitch addTarget:self action:#selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
[cell.contentView addSubview:privateSwitch];
//[privateSwitch setOn:NO animated:NO];
if ([howToDisplay isEqualToString:#"no"]) {
[privateSwitch setOn:NO animated:NO];
} else {
[privateSwitch setOn:YES animated:NO];
}
[cell.contentView addSubview:myLabel];
} else {
// Set up loading text and show it
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 13, 280, 20)];
myLabel.text = #"Comments";
myLabel.textColor = [UIColor blackColor];
myLabel.textAlignment = UITextAlignmentLeft;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.font = [UIFont fontWithName:#"Helvetica" size: 16.0];
myLabel.numberOfLines = 0;
//[myLabel sizeToFit];
[cell.contentView addSubview:myLabel];
commentsSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[cell.contentView addSubview:commentsSwitch];
[commentsSwitch setOn:YES animated:NO];
}
} else {
//cell.text = cellValue;
UILabel *labelOne = [[UILabel alloc] initWithFrame:CGRectMake(48, 12, 130, 20)];
labelOne.text = cellValue;
labelOne.textColor = [UIColor blackColor];
[labelOne setFont:[UIFont boldSystemFontOfSize:16]];
labelOne.textAlignment = UITextAlignmentLeft;
labelOne.backgroundColor = [UIColor clearColor];
//labelOne.font = [UIFont fontWithName:#"Helvetica"];
labelOne.numberOfLines = 0;
[cell.contentView addSubview:labelOne];
if (indexPath.row == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else if (indexPath.row == 1) {
int countFacebook = [dataCeter.connectionFacebookArray count];
if (countFacebook == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
} else if (indexPath.row == 2) {
//} else if (indexPath.row == 3) {
} else if (indexPath.row == 3) {
int countTumblr = [dataCeter.connectionTumblrArray count];
if (countTumblr == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
} else {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
} else if (indexPath.row == 4) {
} else if (indexPath.row == 5) {
} else {
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
}
// Set imageView with correct thumbnail
UIImage *theImage;
if ([cellValue isEqualToString:#"Facebook"]) {
theImage = [UIImage imageNamed:#"icon_small_facebook.png"];
int countFacebook = [dataCeter.connectionFacebookArray count];
NSLog(#"facebook? %d // %#", countFacebook, dataCeter.connectionFacebookArray);
if (countFacebook != 0) {
facebookSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[cell.contentView addSubview:facebookSwitch];
[facebookSwitch setOn:YES animated:NO];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
} else if ([cellValue isEqualToString:#"Twitter"]) {
theImage = [UIImage imageNamed:#"icon_small_twitter.png"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if ([cellValue isEqualToString:#"Flickr"]) {
theImage = [UIImage imageNamed:#"icon_small_flickr.png"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if ([cellValue isEqualToString:#"Tumblr"]) {
theImage = [UIImage imageNamed:#"icon_small_tumblr.png"];
int countTumblr = [dataCeter.connectionTumblrArray count];
if (countTumblr != 0) {
tumblrSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[cell.contentView addSubview:tumblrSwitch];
[tumblrSwitch setOn:YES animated:NO];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
} else if ([cellValue isEqualToString:#"Email"]) {
theImage = [UIImage imageNamed:#"icon_small_email.png"];
int countEmail = [dataCeter.connectionEmailArray count];
} else if ([cellValue isEqualToString:#"MMS"]) {
theImage = [UIImage imageNamed:#"icon_small_mms.png"];
int countMMS = [dataCeter.connectionSMSArray count];
} else if ([cellValue isEqualToString:#"Photostream"]) {
theImage = [UIImage imageNamed:#"icon_small_photostream.png"];
cell.accessoryType = UITableViewCellAccessoryNone;
photostreamSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(199, 8, 0, 0)];
[cell.contentView addSubview:photostreamSwitch];
[photostreamSwitch setOn:YES animated:NO];
} else {
theImage = nil;
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.imageView.image = theImage;
return cell;
}
Set your table view's separator style to UITableViewCellSeparatorStyleSingleLine. It's currently being set to UITableViewCellSeparatorStyleSingleLineEtched, which gives the effect of a doubled top border on the iPhone (it looks more detailed on iOS 5, and on iOS 3.2 and 4 on the iPad).
You're not insane, it looks like there is an extra pixel in there.
Try taking out "Sharing" and see if it still happens. Curious to see if the shadow is on "Sharing" or the table itself.
If that's the case, then you know your header view has a problem, not the table view.

UITextField in UITableView cell is returning null

I've been banging my head against the wall on this one for quite some time now. Any input or direction is greatly appreciated.
So the goal is the create a log in form from text fields in a table. That user info, once collected will be passed on to an array in a seperate view controller so in can stored in a "favourites" list.
So I've created the form which looks great and all but when I console out the form results the fields are return (null). I've picked the site for answers but can't anything exact. Below is the cellForRowAtIndexPath: method I feel that maybe the issue is where I'm creating the textFields. Again, any input is appreciated!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
[textField retain];
}
textField.adjustsFontSizeToFitWidth = NO;
textField.font = [UIFont fontWithName:#"Helvetica" size:14.0];
textField.textColor = [UIColor darkGrayColor];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.textAlignment = UITextAlignmentLeft;
textField.clearButtonMode = UITextFieldViewModeNever;
textField.delegate = self;
if ([indexPath section] == 0) {
switch (indexPath.row) {
case 0:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 0;
break;
case 1:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 1;
break;
case 2:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 2;
break;
case 3:
textField.placeholder = #"";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 3;
break;
case 4:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
textField.tag = 4;
break;
case 5:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.tag = 5;
break;
}
}
[textField setEnabled: YES];
[cell addSubview:textField];
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editing = YES;
return cell;
}
The textField variable (I assume it is an ivar in the class or a static global variable) is your main problem. You create a new text field each time you create a new cell, which is fine, but then you add it to a cell every time the cellForRowAtIndexPath method is called. Since cells are reused this will screw things up.
Your code need to look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.editing = YES;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 13, 375, 30)];
textfield.tag = 1;
textField.adjustsFontSizeToFitWidth = NO;
textField.font = [UIFont fontWithName:#"Helvetica" size:14.0];
textField.textColor = [UIColor darkGrayColor];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.textAlignment = UITextAlignmentLeft;
textField.clearButtonMode = UITextFieldViewModeNever;
textField.delegate = self;
[textField setEnabled: YES];
[cell.contentView addSubview:textField];
[textField release];
}
UITextField *textField = (UITextField *) [cell.contentView viewWithTag:1];
if ([indexPath section] == 0) {
switch (indexPath.row) {
case 0:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 1:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 2:
textField.placeholder = #"";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 3:
textField.placeholder = #"";
textField.secureTextEntry = YES;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 4:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeDefault;
break;
case 5:
textField.placeholder = #"Optional";
textField.secureTextEntry = NO;
textField.keyboardType = UIKeyboardTypeNumberPad;
break;
}
}
textField.text = [listData objectAtIndex:indexPath.row];
return cell;
}
Not sure this does exactly what you want but it should point you in the right direction.
Create a custom cell to place your text field, for the love of god. You shouldn't have addSubview: related code in your tableView:cellForRowAtIndexPath:; just code that allocates the cell, and configures the cell enough so that the cell itself can display things they way you want them.
Look at the table view suite for an example of how to use custom cells. I believe 4_ and 5_ have custom cells.