Determinar el tamaño de UITableViewCell

I am trying to dtermine the width of the cell that I'm in when i am creating my thumbnails (it's for an app very similar to the photos app on the iphone).

Esto es lo que tengo:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
int photoWidth = self.frame.size.width / 4;
    thumbnail1 = [[ThumbnailButton alloc] initWithFrame:CGRectMake(0, 0, photoWidth, 40) ];
    thumbnail2 = [[ThumbnailButton alloc] initWithFrame:CGRectMake(photoWidth, 0, photoWidth, 40) ];
    thumbnail3 = [[ThumbnailButton alloc] initWithFrame:CGRectMake(photoWidth * 2, 0, photoWidth, 40) ];
    thumbnail4 = [[ThumbnailButton alloc] initWithFrame:CGRectMake(photoWidth * 3, 0, photoWidth, 40) ];
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:thumbnail1];
        [self.contentView addSubview:thumbnail2];
        [self.contentView addSubview:thumbnail3];
        [self.contentView addSubview:thumbnail4];
    return self;

My main problem is with:

int photoWidth = self.frame.size.width / 4;

I cannot figure out the height and width of the Table Cell.

Any help would be appreciated, Cheers, Stefan

preguntado el 09 de enero de 11 a las 00:01

not your question really, but there's some memory leaking here. Add [... autorelease] to all those alloc declaraciones. -

Thank you for catching that! I'm still fairly new to Objective-C -

Also at this point you're trying to create a grid no doubt, in which case, don't do it this way. Utilize existing mature code. AQGridView would be a good fit here, all you'd have to do is shove your image into a cell, and if the number of cells * your cell width doesn't divide evenly against your superview width, just adjust the content offset so everything lines up right. Very easy to use. -

1 Respuestas

At this point the table cell which is in the making cannot know how big it will be, unless you tell it. It also has no reference to the tableview it will be added to, so there is no way.

You could just use some size, set the appropriate resizing masks and when adding the cell, determine the frame with dimensions from tableView.frame.size.

Respondido el 09 de enero de 11 a las 03:01

Could you elaborate a little more on " set the appropriate resizing masks and when adding the cell, determine the frame with dimensions from tableView.frame.size" - Stefan H

Just pick some size (preferably: the correct size) and make the table cell perfect for that size. Then later on, when the cell is set up and ready to be returned from tableView:cellForRowAtIndexPath:, set the true size. You can get the true size from the tableview, of course. Because you will probably add the table-view-cell to a table-view. Right? - mvds

regarding the resizing mask, have a read at… "When creating a view, it is important to assign an appropriate value to the autoresizingMask property to ensure the view resizes correctly." - mvds

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.