开发者

Get width/frame of entire UITableViewCell

开发者 https://www.devze.com 2023-03-22 17:55 出处:网络
I have a method where I have a (rendered) UITableViewCell*. I would like to know the width/frame of the entire cell.

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 w开发者_JS百科ork 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?


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.


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);


cell.frame will always give you the frame of the cell you're messaging.

And even if it did return a random cell, you should still be able to use the width of the frame, because all the cells have the same width.


I think by default, a cell will use 100% of the width of the UITableView so a UITableView's width is equivalent to a cell's width and because you would likely have set your table cell height using:

[UITableView setRowHeight:tableCellHeight];

Where tableCellHeight is a number of your choice and UITableView is the name of your UITableView not the UITableView class itself.

Then you would have both the width and height of a table cell, or did I misinterpret your question?

0

精彩评论

暂无评论...
验证码 换一张
取 消