UITableViewCell custom background becomes transparent after selection

When I select and hold one of my user cells for a table UITableViewin the usual style, it displays my custom selection color ( redColorin this case), but if I hold it down and scroll, the user selection color and user background disappear (showing UIImageViewbehind the table.

UITableViewis created in IB and has a background set to clearColorso that the image is displayed through.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                         reuseIdentifier:kCellIdentifier] 
                autorelease];

        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

        /* Background */
        cell.backgroundView = [[[UIImageView alloc] init] autorelease];
        cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
    }

    cell.backgroundView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"customBackground"]];
    cell.selectedBackgroundView.backgroundColor = [UIColor redColor];

    return cell;
}

Can anyone help? Thank!

+3
source share
1 answer

, . initWithPatternImage UITableViewCell. , , .

:

((UIImageView*)cell.backgroundView).image = [UIImage imageNamed:@"customBackground"];
((UIImageView*)cell.selectedBackgroundView).image = [UIImage imageNamed:@"selectedBackground"];
+4

Source: https://habr.com/ru/post/1772777/


All Articles