开发者

Offsetting UIButton frames in a tableview row

Inside my cellForRowAtIndexPath method, I would like to dynamically create some buttons and place them next to each other in the cells. The problem I'm running into is that the sizeToFit method always puts the x coordinate at the 0 position. Is there a way to offset the position to put it next to the previous button?

Instead of this:

Offsetting UIButton frames in a tableview row

. I get this:

Offsetting UIButton frames in a tableview row

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)in开发者_运维百科dexPath {


UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:@"cell"] autorelease];
    cell.accessoryType = UITableViewCellAccessoryNone;

    if ([indexPath section] == 0) {
        UIButton *buttonClean = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [buttonClean addTarget:self 
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchDown];
        [buttonClean setTitle:@"Clean Cup" forState:UIControlStateNormal];
        [buttonClean sizeToFit];
        [cell addSubview:buttonClean];

        UIButton *buttonAroma = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [buttonAroma addTarget:self 
                   action:@selector(aMethod:)
         forControlEvents:UIControlEventTouchDown];
        [buttonAroma setTitle:@"Aroma" forState:UIControlStateNormal];
        [buttonAroma sizeToFit];
        [cell addSubview:buttonAroma];


You should position UIViews by setting their frame property.

This is an option:

[buttonAroma setFrame:CGRectMake(buttonClean.frame.size.width,0,buttonAroma.frame.size.width,buttonAroma.frame.size.height)];

you should also add your views to the cell's contentView, instead of adding them to the cell directly:

[cell.contentView addSubview:buttonAroma];

See UIView's frame property here:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/frame

See UITableViewCell's contentView property here:

http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/contentView

.. where it says:

If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.


It's not the sizeToFit method that's putting them at y=0, it's the fact that you never set any other position. You can move each button by assigning its center or frame properties; for the latter, you would of course want to read out the property, modify just the origin of the CGRect, and set it back to the property so as to not mess up the sizing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜