Reputation: 3
im trying to build apps that have tableview cell similiar with twitter, there are text and images. My problem is i want to insert the image above the text. While it can be text only. So if there are no image, the text will be in position (0,0). And if there are images it will be (0,306) position.
Do i have to use programatically or using autolayout?
Appreciate for the answer. Thanks..
Upvotes: 0
Views: 1307
Reputation: 3358
Try this
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//calculating the height according to the device height.
return self.view.frame.size.height * (102.0 / 568.0);
}
Where 102.0 will be the current height according to the current design xib height 568.0. This will resize the cell according to the height.
Upvotes: 0
Reputation: 1206
Using AutoLayout, just take an NSLayoutConstraint and bind it to the Top Space constraint of the label. Also you need to calculate the size of the label, to adjust cell height based on content.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//If image exists, set the value of the constraint to 306, else set it to 0
constraintName.constant = 306;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat labelHeight = [self findHeightForText:labelMe.text havingWidth:labelWidth andFont:[UIFont systemFontOfSize:22];
return labelHeight + paddingHeight;
}
//Method to calculate label height
- (CGFloat)findHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
#ifdef __IPHONE_7_0
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
#else
size = [text sizeWithFont:font constrainedToSize:CGSizeMake(widthValue, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
#endif
}
return size.height;
}
Upvotes: 0
Reputation: 3984
If use AutoLayout the use this code
You try to set the UITableView
properties estimatedHeight
.
-(void)viewWillAppear:(BOOL)animated{
_messageField.delegate = self;
_tableView.estimatedRowHeight = 65.0;
_tableView.rowHeight = UITableViewAutomaticDimension;
}
Other wise use this UITableView
Delegate Method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 300; // your dynamic height...
}
Upvotes: 1
Reputation: 347
It is better to do it programmatically use this delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Upvotes: 0