开发者

Only bottom tableviewcell background being edited

开发者 https://www.devze.com 2023-04-01 10:22 出处:网络
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 i

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.d开发者_如何学CetailTextLabel.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 X as a subView of another view A, then A will contain the view X. Later, if you add the same view X to some other view B, then X will be first removed from A and added to B. That is what happening here.

Here , you are having a view backgroundImage and assigning it as the backgroundView of all the cells. So, obviously the lastly assigned cell will contain the backgroundImage.

You have to create new views in cellForRowAtIndexPath: method and assign it to the current cell.

UIImageView *backgroundImage = ...
// Configure backgroundImage
[cell setBackgroundView:backgroundImage];
0

精彩评论

暂无评论...
验证码 换一张
取 消