UITableViewCell - how to change selectedBackgroundView, default ImageView, textLabel, detailTextLabel frames

I am creating a tableview with a row height of 100, I want 5px between each row. To do this, for each line, I add a uiview (width: 320px, hight: 1px) on line 95th 'y'. Everything is going well now. But when I select a cell, the rows select backgroundview spanning the entire i row. But I want to set the row's selectedbackgroundview height to 95.

I have tried the following path in cellForRowAtIndexPath

but not used, please help me.

cell.selectedBackgroundView.frame = CGRectMake (0, 0, 320, 95);



source to share

2 answers

Thanks for your answer, finally got a solution.

We can adjust the selected checkbox frame by creating a custom cell. Steps to solve the problem.

  • First we have to create a class with UiTableViewCell as a subclass
  • Inside this method, layoutSubviews

    we can modify the presentation of the frame associated with tableviewcell: selectedBackgroundView

    , defalut imageView

    , textLabel

    , detailTextLabel


    To change the selected frame ddackbackView

     - (void )layoutSubviews {
    // always try to set frame in layoutSubviews
    [super layoutSubviews];
    self.selectedBackgroundView.frame = CGRectMake(10, 0, self.frame.size.width - 20, 88);

    To change textLabel, detailTextLabel Frames

        - (void)layoutSubviews {// always try to set frame in layoutSubviews
        [super layoutSubviews];
        self.textLabel.frame = CGRectMake(0, 0, 50, 20);
        self.detailTextLabel.frame = CGRectMake(55, 0, 225, self.frame.size.height);



Just a thought

  • You can use multiple sections of the tableview pass array.Count in numberOfSectionsInTableView

  • numberOfRowsInSection

    each section will have 1

  • Move 5px in heightForHeaderInSection

    so that between each of the lines

  • Pass 95 px to heightForRowAtIndexPath

    whatever you want the cell height to be 95,

  • In cellForRowAtIndexPath

    instead indexPath.row

    you have to deal withindexPath.section

Let me know if I am missing something. I think it will do what you want to achieve. Hope it helps.



All Articles