Only the bottom cell of my UITableView is being edited when i change the backgroundView. I want the same background for each cell. But top 3 are white and last one is the image. I dont know why this is. Here is my code under the tableView cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
}
[cell setBackgroundView:backgroundImage];
[cell.detailTextLabel setFont:[UIFont boldSystemFontOfSize:15]];
[cell.detailTextLabel setTextColor:[UIColor blackColor]];
[cell.textLabel setFont:[UIFont systemFontOfSize:10]];
[cell.textLabel setTextColor:[UIColor darkGrayColor]];
cell.detailTextLabel.text = [packArray objectAtIndex:[indexPath row]];
cell.textLabel.text = @"Soleternity Productions";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.shadowColor = [UIColor grayColor];
cell.detailTextLabel.shadowColor = [UIColor grayColor];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
return cell;
The concept is simple. If you add a view
Xas a subView of another viewA, thenAwill contain the viewX. Later, if you add the same viewXto some other viewB, thenXwill be first removed fromAand added toB. That is what happening here.Here , you are having a view
backgroundImageand assigning it as thebackgroundViewof all the cells. So, obviously the lastly assigned cell will contain thebackgroundImage.You have to create new views in
cellForRowAtIndexPath:method and assign it to the current cell.