Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get width/frame of entire UITableViewCell

I have a method where I have a (rendered) UITableViewCell*. I would like to know the width/frame of the entire cell.

cell.frame doesn't seem to work - it gives me the frame of a random cell. The closest I got is cell.contentView.frame but it doesn't include the area covered by the accessoryView (see http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7). cell.contentView.superview.frame doesn't work either - it gives me a frame at some arbitrary location just like cell.frame does.

cell.contentView.width + cell.accessoryView.width works except in cases where the accessory view is UITableViewCellAccessoryNone

Any ideas how I can get the entire frame/width of the UITableViewCell in all cases?

like image 320
psychotik Avatar asked Dec 02 '22 01:12

psychotik


2 Answers

Try this,

NSLog(@"Cell Width = %f",cell.frame.size.width);
NSLog(@"Cell Height = %f",cell.frame.size.height);

It will show the current cell Width and Height.

like image 119
Sisu Avatar answered Dec 18 '22 00:12

Sisu


This code gives you the frame of the particular cell selected by indexPath:

 // Get the cell rect and adjust it to consider scroll offset
    CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
    cellRect = CGRectMake(cellRect.origin.x - tableView.contentOffset.x, cellRect.origin.y - tableView.contentOffset.y, cellRect.size.width, cellRect.size.height);
like image 29
Tomasz Avatar answered Dec 17 '22 23:12

Tomasz