Reputation: 365
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
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
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
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