0
votes

I'm coding a custom UITableViewCell object and I've implemented layoutSubviews to resize the cell when its contents is updated.

To implement heightForRowAtIndexPath, I'm calculating the height of the cell within the custom UITableViewCell object. I'm using NSString sizeWithFont to calculate the size of the cell based on the text within a UILabel and the width of a cell in the UITableView.

But how do I get the width of a cell in the UITableView?

Right now, I'm passing in the table view to the object and using the frame. But the width is of the whole table not the individual cells. Is there something I'm missing here?

Thanks in advance.

EDIT3

I was thinking, if it's really not possible, just test portrait or landscape orientation then set the width manually (ipad just has 2 different orientations)..

EDIT2

There have been some questions about 'why are you doing it this way xxxx'. I understand maybe there's a better way to achieve what I'm doing, creating a custom cell that can calculate its own height with varying text length. If there's a better way of doing it I'm all ears :)

EDIT

http://img845.imageshack.us/img845/7792/screenshot20121129at193.png

+ (CGFloat)getHeightForCellWithText:(NSString *)text isExpanded:(BOOL)expanded tableView:(UITableViewController *)tv {

ProposalViewCell *cell = [[ProposalViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"NormalCell" tableView:tv];

[cell setLabelText:text];

[cell setExpanded:expanded];

[cell layoutSubviews];

return cell.primaryLabel.frame.size.height + cell.readmoreButton.frame.size.height + cell.sendmessageButton.frame.size.height +30;

}


- (void)layoutSubviews {

[super layoutSubviews];

_primaryLabel.numberOfLines = 0; // multiple lines

// size of expanded text label
// sizeWithFont: if text doesn't fit, it is truncated
CGSize expandedSize = [_primaryLabel.text sizeWithFont:myFont constrainedToSize:CGSizeMake(tableView.view.frame.size.width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];

// size as expanded by default
_primaryLabel.frame = CGRectMake(10, 10, expandedSize.width, expandedSize.height);

if (expanded==NO) {

    // size of summary text label
    _primaryLabel.numberOfLines = 10;
    CGSize summarySize = [_primaryLabel sizeThatFits:_primaryLabel.frame.size];

    _primaryLabel.frame = CGRectMake(10, 10, summarySize.width, summarySize.height);

}

_readmoreButton.frame = CGRectMake(10, _primaryLabel.frame.size.height+10, 225, 25);

_sendmessageButton.frame = CGRectMake(10, _primaryLabel.frame.size.height+10+_readmoreButton.frame.size.height+10, 225, 25);

}
1
Why would you want a cell that has a different width than the tableview?jimpic
I dont. But when a cell is displayed in a UITableView, the width of the cell is not the width of the tableview itself. It is less than the tableview. There is some space between the edge of the cell and the tableview edge (which occupies the whole screen). I just want to somehow get the width of the individual cell and not the entire tableview, so I can size the text. Hope that explains the problem!Imme22009
Have you tried cell.contentView.frame ?Cyupa

1 Answers

0
votes

To get the width of the cell just do this...

cell.contentview.frame.size.width