DominicanCoder
DominicanCoder

Reputation: 365

Changing position and size of UILabel in custom UITableViewCell

I am trying to change the position and size of a UILabel inside my custom UITableViewCell depending on a certain condition being true. In my custom UITableViewCell header file, I have a UILabel declared as such:

@property (strong, nonatomic) IBOutlet UILabel *stepText;

I want to change the location an size of the UILabel based on a condition. In the view that holds my UITableView I perform the check as such:

if(step.picture == nil){
    CGRect frame = cell.stepText.frame;
    frame = CGRectMake(0, 0, 100, 100);
    cell.stepText.frame = frame;
}

I have used an NSLog inside that check and I have verified that it gets executed when ever the condition is true, however, the location and size of my label in my custom cell does not change.

What are my doing wrong here?

Upvotes: 0

Views: 3385

Answers (3)

Clay Bridges
Clay Bridges

Reputation: 11880

You might try -setNeedsLayout after you change anything. Doing that with a simplified version of your code above yields:

if (step.picture == nil) {
    cell.stepText.frame = CGRectMake(0, 0, 100, 100);
    [cell setNeedsLayout];
}

Better yet, you could put your re/sizing code in -layoutSubviews, and call [cell setNeedsLayout] when anything changes. On the relationship of these two methods, confer this SO answer, or many other sources.

Upvotes: 2

Jiten Parmar
Jiten Parmar

Reputation: 164

It will be better if you use the create uitableviewcell and it's controller outlet to the appropriate controls on it and use it like custom tableview.

Upvotes: 0

ares777
ares777

Reputation: 3628

Make sure you add custom UILabel to cell.contentView.

If you want custom label use:
- in file .h

@property (nonatomic, retain) IBOutlet UILabel *ttitle;

-in file .m

self.ttitle = [[UILabel alloc] initWithFrame:CGRectMake(74,16,190,16)];
self.ttitle.textAlignment = NSTextAlignmentLeft;
[self.ttitle setText: [NSString  stringWithFormat: @"yourtext"]];
[cell.contentView addSubview:self.ttitle];

Hope this helps.

Upvotes: 0

Related Questions