4

I am creating tableview with row height 100, between each row i want a 5 pixels gap. For that for each row i am adding uiview(width:320px, hight:1px) at row's 95th 'y' position . Now everything is going fine. But when i select the cell the rows selectedbackgroundview covering entire row i. But i want to set rows selectedbackgroundview height as 95.

i tried following way in cellForRowAtIndexPath but no use, please help me out.

cell.selectedBackgroundView.frame = CGRectMake (0, 0, 320, 95);
5
  • You need to reload the table and return the height in the heightForRowAtIndexPath: delegate method.
    – ZeMoon
    Commented Sep 15, 2014 at 18:50
  • stackoverflow.com/a/5784367/2043580
    – ZeMoon
    Commented Sep 15, 2014 at 18:51
  • i am using 'heightForRowAtIndexPath', for row height Commented Sep 15, 2014 at 18:51
  • You mean you designed tableview that cells show 5 px gap in between them but , when cell is tapped selection effect covers whole cell show you can see the gap? Commented Sep 15, 2014 at 18:55
  • @Bhumit: yes, when i tap the cell the selection covering entire cell(height:100px), but in this case selection has to cover 95 px. Commented Sep 15, 2014 at 19:00

3 Answers 3

14

Thanks for your answer, Finally got the solution.

We can adjust the selectedBackgroundView frame by creating the custom cell. Steps to solve the problem.

  1. First we have to create class with UiTableViewCell as a Subclass
  2. Inside that under layoutSubviewsmethod we can change the tableviewcell related view frames those are selectedBackgroundView, defalut imageView, textLabel, detailTextLabel frames

    To change the selecteddBackgroundView frame

     - (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 the 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);
    
       }
    
3

Swift:

override func layoutSubviews() {
    super.layoutSubviews()
    // containerView -> Is a view with constraints in the storyboard where I whish to see the selection
    selectedBackgroundView?.frame = containerView.frame
}
0

Just a thought

  1. You can use multiple sections tableview pass array.Count in numberOfSectionsInTableView

  2. numberOfRowsInSection in each section will be 1

  3. Pass 5px in heightForHeaderInSection so that there will be 5px gap between each of the rows

  4. Pass 95 px to heightForRowAtIndexPath as you want height of cell to be 95 ,

  5. In cellForRowAtIndexPath instead of indexPath.row you will have to deal with indexPath.section

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

2
  • Thanks for your answer but found the solution. Commented Sep 15, 2014 at 20:19
  • Glad you did :) Can you share it here Commented Sep 15, 2014 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.