0

I have a grouped tableView in my iPad-app, and I've been trying to set cell.imageView.center = cell.center to center the image instead of putting it to the leftmost position. This is apparently not possible without a subclass of the UITableviewCell(If someone could explain why, that'd also be appreciated.. For now I just assume they are 'private' variables as a Java-developer would call them). So, I created a custom tableViewCell, but I only want to use this cell in ONE of the rows in this tableView. So in cellForRowAtIndexPath I basically write

cell = [[UITableViewCell alloc]initWith//blahblah

if(indexPath.row == 0)
    cell = [[CustomCell alloc]initWith//blahblah

This is of course not exactly what I'm writing, but that's the idea of it.

Now, when I do this, it works, but the first cell in this GROUPED tableView turns out wider than the rest of them without me doing anything in the custom cell. The customCell class hasn't been altered yet. It still has rounded corners though, so it seems it knows it's a grouped tableView.

Also, I've been struggling with programmatically getting the size of a cell, in cellForRowAtIndexPath, I've tried logging out cell.frame.size.width and cell.contentView.frame.size.width, both of them returning 320, when I know they are a lot wider.. Like, all the rows are about 400 wide, and the first cell is 420 or something. It still writes out 320 for all the cells..

Sti
  • 8,275
  • 9
  • 62
  • 124

1 Answers1

0

This code will not work for a couple of reasons:

cell.imageView.center = cell.center;

Firstly, the center is relative to its superview. I believe the cells superview is the tableView. The imageView's superview will be the content view of the cell. Therefore the coordinate systems are different so the centens will be offset. E.g. the 3rd cell down will have a center of 0.5 widths + 3.5 heights. You should be able to ge around this issue by doing:

cell.imageView.center = CGPointMake( width / 2 , height / 2 );

The second issue is related to how the table view works. The table view manages its cells view's. The width of a cell is defined by the table view's width and the height is defined by the table view's row height property. This means the cell itself has no control over its size.

You can however size its subviews, but you must do this after the cells size has been set (otherwise you can get strange results). You can do this in layout subviews (of the custom UITableViewCell class). See this answer.

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = ....
}

When layoutSubviews is called the cells frame has been set, so do your view logging here instead of cellForRowAtIndexpath.

As for the GROUPED style. Im not sure if this is designed to work with custom views. I suspect it sets the size of its cells to its own width minus a 20 pixel margin on each size, then applies a mask to the top and bottom cells in a section to get the rounded effect. If you are using custom view try to stick with a standard table view style.

Community
  • 1
  • 1
Robert
  • 37,670
  • 37
  • 171
  • 213
  • So by setting `cell.imageView.center.x = tableView.center.x`, that should solve my problem? But it doesnt.. – Sti Jan 11 '13 at 19:01
  • Are you doing this in the layoutSubview's method of the custom tableviewcell (not in the table view) – Robert Jan 11 '13 at 19:16
  • No, in cellForRowAtIndexPath.. Centering the imageView is the only reason why I'm creating the custom tableViewCell, so I'm still trying to figure out how to do it without actually subclassing the cell.. – Sti Jan 11 '13 at 19:20
  • Especially now that the custom tableViewCell looks really stupid. I haven't changed the width of the cell, but it is much wider than the others. – Sti Jan 11 '13 at 19:20
  • is cell.imageView's location etc unsettable from outside the tableViewCell class? So I HAVE to subclass it? – Sti Jan 11 '13 at 19:24