简体   繁体   English

在表格视图行中偏移UIButton框架

[英]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. 在我的cellForRowAtIndexPath方法内部,我想动态创建一些按钮并将它们在单元格中彼此相邻放置。 The problem I'm running into is that the sizeToFit method always puts the x coordinate at the 0 position. 我遇到的问题是sizeToFit方法始终将x坐标置于0位置。 Is there a way to offset the position to put it next to the previous button? 有没有办法偏移位置以将其放置在上一个按钮旁边?

Instead of this: 代替这个: 所需效果 . I get this: 我得到这个: 不想要的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


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. 您应该通过设置UIView的frame属性来放置它们。

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: 您还应该将视图添加到单元格的contentView中 ,而不是将视图直接添加到单元格中:

[cell.contentView addSubview:buttonAroma];

See UIView's frame property here: 在此处查看UIView的frame属性:

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/frame 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: 在这里查看UITableViewCell的contentView属性:

http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/contentView 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. 不是将它们设置为y = 0的sizeToFit方法,而是您从未设置任何其他位置的事实。 You can move each button by assigning its center or frame properties; 您可以通过分配每个按钮的centerframe属性来移动它们。 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. 对于后者,您当然希望读出该属性,仅修改CGRect的来源,然后将其设置回该属性,以免弄乱大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM