Reputation: 1059
I have a Class MTTableViewCell : UITableViewCell The init method in the class is as follows: Notice that I set the backgroundcolor to purple.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
[self setBackgroundColor:[UIColor purpleColor]];
}
return self;
// return [self initMVTableViewCellWithStyle:style reuseIdentifier:reuseIdentifier cellColor:nil];
}
I call the following method from the delegate of the tableview
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifier = @"one";
[self.tableView registerClass:[MVTTableViewCell class] forCellReuseIdentifier:identifier];
MVTTableViewCell *cell = [[MVTTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.textLabel.text = [self->datasource objectAtIndex:[indexPath row]];
return cell;
}
However I do not see any change in the color of the table view cells. What is going wrong ?
Upvotes: 1
Views: 294
Reputation: 5754
follow this... for custom tableview cell
UITableViewCell Space between Title and Subtitle -iOS
http://www.appcoda.com/customize-table-view-cells-for-uitableview/
Upvotes: 0
Reputation: 130193
I would try moving your call to register class to your viewDidLoad method, and instead of alloc/initing the cell, try dequeueing one from the table. By registering the cell's class for reuse, you're preparing it for recycling by the table. Note: be sure that the cell id that you register is the same as the one that you access in cellForRowAtIndexPath:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[MVTTableViewCell class] forCellReuseIdentifier:@"one"];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"one";
MVTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
cell.textLabel.text = [self->datasource objectAtIndex:[indexPath row]];
return cell;
}
Unfortunately I'm not on a machine where I can test this an I can't seem to remember the cell call structure in this case (it's late :)) But I would check to see if maybe a different init is being called, otherwise, try setting the background color in cellFor... itself just to troubleshoot. Try setting it on the contentView of the cell as well.
Upvotes: 1
Reputation: 2451
When I want to change the background color of my cell i usually use this:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self)
{
[self.contentView setBackgroundColor:[UIColor purpleColor]];
}
return self;
}
Upvotes: 1