开发者

Cocoa iOS/Touch: Keeping a subview of the UITableViewCell contentView right-aligned

Let's say I have a UITableView that

  1. is part of an iPad app
  2. doesn't use the whole screen
  3. changes its width, depending on the state of the app and other views

I added an extra subview (let's say an UILabel) to the UITableView's cell's contentView. I want the UILabel's right border to be 10 pixels from the right cell border at all times.

I managed to do this for an initial drawing of the table view for any width by specifically setting the cells frame after dequeuing it which also updates the contentVi开发者_高级运维ew's width (otherwise the cells seem to be 320 pixels wide all the time).

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];

if (cell == nil) {
    cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
    CGRect oldFrame = cell.frame;
    cell.frame = CGRectMake(oldFrame.origin.x, oldFrame.origin.y, tableview.frame.size.width, oldFrame.size.height);

    /* and other stuff */
}

Question: How can I keep the subviews of the cell's contentView aligned to the cell's right border when the table view's size changes at runtime?


You could try setting your cell autoresizingMask and its autoresizesSubviews properties?

Possibly this would work:

cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
cell.contentView.autoresizesSubviews = YES;

you should also set the autoresizingMask of your UILabel, I think:

label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;

Here the docs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜