subclass uitableviewcell and override layoutsubviews

11,919

Solution 1

All I need was to set frame with numbers.

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.selectedBackgroundView.frame = CGRectMake(10.0f, 0, 300, 80);

}

Solution 2

Instead add the background in the subclassed cell itself, do like this it is an example how you can manage the selection and deselection state in the cell change it to your requirements

   //in your subclassed cell
   #import "GroupedTableCell.h"

   @implementation GroupedTableCell

   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
      if (self) {
       // Initialization code
       //add the background view hear only, changes the frame in the layoutsubviews
       UIView *cellBgView = [[UIView alloc] init];


     [cellBgView setTag:12345];//using tag to access in the layoutsubviews
     [self addSubview:cellBgView];//hear u added the background view



    }
    return self;
 }

 //manage the cell selection and deselection state hear
  - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
   // Configure the view for the selected state  
   [super setSelected:selected animated:animated];
    UIView *cellBgView = [self viewWithTag:12345];
    if(selected)
    {

      [cellBgView setBackgroundColor:[UIColor colorWithRed:242 / 255.0 green:242 / 255.0 blue:242 / 255.0
                                                   alpha:1.0]]; //your selected background color
    }
    else
   {
       [cellBgView setBackgroundColor:[UIColor clearColor]]; //your deselected background color

   }

}

//setting the frames of views within the cell
- (void)layoutSubviews
{
    [super layoutSubviews];
    UIView *cellBgView = [self viewWithTag:12345];
    cellBgView.frame = CGRectMake(10, 0, 300, 80);//always set the frame in layoutSubviews

}


in your controller just do like this


  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      GroupedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
      if(cell == nil)
      {
          cell = [[GroupedTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
      }

     return cell;

 }

Hope this helps u :)

Share:
11,919
Kisel Alexander
Author by

Kisel Alexander

I have 5 years of software development experience on the iOS platform. Developed more than 30 iPhone and iPad applications. I have worked with various aspects of iOS platform using different frameworks for resolving long range of application tasks. I am always striving to explore new technologies and look to ways to improve my skills in this rapidly changing industry.

Updated on June 04, 2022

Comments

  • Kisel Alexander
    Kisel Alexander almost 2 years

    I need to make cell selectionBackgroundView not on full width in plain UItableView. For this I made UItableViewCell subclass and override layoutsubviews method

    - (void)layoutSubviews
    {
        [super layoutSubviews];
    
        self.selectedBackgroundView.frame = CGRectMake(self.frame.origin.x + 10.0f, self.frame.origin.y, self.frame.size.width - 20.0f, self.frame.size.height);
    
    }
    

    My cellForRowAtIndexPath method looks like

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *identifier = [NSString stringWithFormat: @"CELL %i %i", indexPath.section, indexPath.row];
        GroupedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if(cell == nil)
        {
            cell = [[GroupedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    
    
            UIView *cellBgView = [[UIView alloc] init];
            cellBgView.frame = CGRectMake(10, 0, 300, 80);
            [cellBgView setBackgroundColor:[UIColor colorWithRed:242 / 255.0 green:242 / 255.0 blue:242 / 255.0 alpha:1.0]];
            [cell.contentView addSubview:cellBgView];
        }
    
    
    
            [cell setBackgroundColor:[UIColor clearColor]];
        UIView *selectionView = [[UIView alloc] init];
        [selectionView setBackgroundColor:[UIColor colorWithRed:181 / 255.0 green:211 / 255.0 blue:53 / 255.0 alpha:1.0]];
        selectionView.frame = CGRectMake(10, 0, 300, 80);
        cell.selectedBackgroundView = selectionView;
        return cell;
    } 
    

    But only for first row selectedView works correct. For other rows I have selectedView with clear color. Please, help me.