mcjoejoe0911 mcjoejoe0911 - 3 months ago 17
iOS Question

Custom UITableViewCell not displaying correctly

I'm trying to create and use a custom UITableView cell in my table view, but it's acting funky. When I run the app the table looks like this:

enter image description here

This is correct. However, when I select a cell, the view shifts to this:

enter image description here

As far as I can tell, it seems after selecting the cell that the cell view shifts to a default UITableViewCell layout with my star image behind it... If I proceed to select each cell I get this:

enter image description here

As you can see, the star image (my custom view) only shows up (albeit screwy) when I select the cell again.

I've gone through Apple's tutorial when trying to make the custom cell so I know I'm doing everything I'm supposed to, but nobody else on StackOverflow has had a similar problem...

Here is the code for my project.

TableView Controller Header

//
// TableViewController.h
// CustomCell
//
// Created by Jordan Gardner on 1/29/14.
// Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController

@end


TableView Controller Implementation

//
// TableViewController.m
// CustomCell
//
// Created by Jordan Gardner on 1/29/14.
// Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import "TableViewController.h"
#import "CustomCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

#pragma mark - Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"Cell";

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

// Configure cell...
cell.textLabel.text = [NSString stringWithFormat:@"Item %@", [NSNumber numberWithInteger:indexPath.row]];
cell.imageView.image = [UIImage imageNamed:@"icon_folder.png"];

return cell;
}

@end


CustomCell Header

//
// CustomCell.h
// CustomCell
//
// Created by Jordan Gardner on 1/29/14.
// Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *favoriteButton;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end


CustomCell implementation

//
// CustomCell.m
// CustomCell
//
// Created by Jordan Gardner on 1/29/14.
// Copyright (c) 2014 Jordan Gardner. All rights reserved.
//

#import "CustomCell.h"

@implementation CustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

@end


So, any suggestions as to how I can prevent the views from acting so screwy would be greatly appreciated. Thanks in advance.

Answer

As mentioned in the question comments by @n00bProgrammer, I was using names like textLabel and imageView that were conflicting with the default values and therefore causing the issues.