tan
tan

Reputation: 97

UITableViewCell Frame Doesn't change after i set in first load

I have created a custom UITableViewCell with xib and add in few UILabel into xib. After that i created my own function to set the frame of the UILabel.

In View Controller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListCell" forIndexPath:indexPath];

[cell setLabelFrame:self.labelFloor.frame
                unit:self.labelUnit.frame
                type:self.labelType.frame
                sqft:self.labelsqft.frame
               price:self.labelPrice.frame
              facing:self.labelFacing.frame
                view:self.labelView.frame
              status:self.labelStatus.frame
         labelHeight:labelHeight];
    }

IN ListTableViewCell:

- (void)setLabelFrame:(CGRect)floor
             unit:(CGRect)unit
             type:(CGRect)type
             sqft:(CGRect)sqft
            price:(CGRect)price
           facing:(CGRect)facing
             view:(CGRect)view
           status:(CGRect)status
      labelHeight:(CGFloat)labelHeight {

float pointX = 0;
CGFloat fontSize = 13;

// Create Floor label
pointX = floor.origin.x;
[self.lb_floor setFrame:CGRectMake(pointX, 0, floor.size.width, labelHeight)];
[self.lb_floor setTextColor:[UIColor blackColor]];
self.lb_floor.textAlignment = NSTextAlignmentCenter;
[self.lb_floor setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

// Create Unit label
pointX = unit.origin.x;
[self.lb_unit setFrame:CGRectMake(pointX, 0, unit.size.width, labelHeight)];
[self.lb_unit setTextColor:[UIColor blackColor]];
self.lb_unit.textAlignment = NSTextAlignmentCenter;
[self.lb_unit setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

// Create Type Label
pointX = type.origin.x;
[self.lb_type setFrame:CGRectMake(pointX, 0, type.size.width, labelHeight)];
[self.lb_type setTextColor:[UIColor blackColor]];
self.lb_type.textAlignment = NSTextAlignmentCenter;
[self.lb_type setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

// Create Sq.Ft. Label
pointX = sqft.origin.x;
[self.lb_sqft setFrame:CGRectMake(pointX, 0, sqft.size.width, labelHeight)];
[self.lb_sqft setTextColor:[UIColor blackColor]];
self.lb_sqft.textAlignment = NSTextAlignmentCenter;
[self.lb_sqft setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

// Create Price Label
pointX = price.origin.x;
[self.lb_price setFrame:CGRectMake(pointX, 0, price.size.width, labelHeight)];
[self.lb_price setTextColor:[UIColor blackColor]];
self.lb_price.textAlignment = NSTextAlignmentCenter;
[self.lb_price setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

// Create Facing Label
pointX = facing.origin.x;
[self.lb_facing setFrame:CGRectMake(pointX, 0, facing.size.width, labelHeight)];
[self.lb_facing setTextColor:[UIColor blackColor]];
self.lb_facing.textAlignment = NSTextAlignmentCenter;
[self.lb_facing setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

// Create View Label
pointX = view.origin.x;
[self.lb_view setFrame:CGRectMake(pointX, 0, view.size.width, labelHeight)];
[self.lb_view setTextColor:[UIColor blackColor]];
self.lb_view.textAlignment = NSTextAlignmentCenter;
[self.lb_view setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

// Create Status Label
pointX = status.origin.x;
[self.lb_status setFrame:CGRectMake(pointX, 0, status.size.width, labelHeight)];
[self.lb_status setTextColor:[UIColor blackColor]];
self.lb_status.textAlignment = NSTextAlignmentCenter;
[self.lb_status setFont: [UIFont fontWithName:@"Helvetica" size:fontSize]];

 }

The problem is, it doesn't change the label frame as i specific in the first load, it changes after i scrolling. How should i refresh it in first laod.

Updated With Images

  1. First Load
  2. After Scrolling

Upvotes: 0

Views: 2300

Answers (2)

Burhanuddin Sunelwala
Burhanuddin Sunelwala

Reputation: 5343

In your method above write the following in the end

- (void)setLabelFrame:(CGRect)floor
             unit:(CGRect)unit
             type:(CGRect)type
             sqft:(CGRect)sqft
            price:(CGRect)price
           facing:(CGRect)facing
             view:(CGRect)view
           status:(CGRect)status
      labelHeight:(CGFloat)labelHeight {

     //setting of frames...
     .
     .
     .
     [self layoutIfNeeded];
}

if AutoLayout is enabled, then use

[self updateConstraintsIfNeeded];

Well on the first hand you shouldn't be touching frames if you are using autolayout. The whole point of using AutoLayout is that frames are inferred based on the satisfiable constraints attached to them.

Or else disable autolayout in your xib and then you can set frames programmatically.

I suggest you should read more on how to use constraints and then you wouldn't be needing to write this whole code for setting frames. AutoLayout would just do that for you.

AutoLayout in iOS7

Upvotes: 2

Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

I hope your cellForRowAtIndexPath should be like this-

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListCell" forIndexPath:indexPath];

        if(cell == nil)
        {
            NSString *nibName = @"ListTableViewCell";
            cell = (ListTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] lastObject];
        }

        [cell setLabelFrame:self.labelFloor.frame
                        unit:self.labelUnit.frame
                        type:self.labelType.frame
                        sqft:self.labelsqft.frame
                       price:self.labelPrice.frame
                      facing:self.labelFacing.frame
                        view:self.labelView.frame
                      status:self.labelStatus.frame
                 labelHeight:labelHeight];

        [cell layoutIfNeeded];
}

Please ensure you are calling your setLabelFrame in both cases

Case 1) Cell created for the first time

case 2) Using dequeue cell

Upvotes: -1

Related Questions