代码之家  ›  专栏  ›  技术社区  ›  Coocoo4Cocoa

用于覆盖UITableViewCell的UI按钮

  •  7
  • Coocoo4Cocoa  · 技术社区  · 16 年前

    我希望我的表行之一是一个按钮,它占据了我的UITableView的整行。我认为最好的方法是实例化一个UIButton,并为其提供与UITableViewCell实例相同的帧大小,然后将其作为子视图添加到单元格中。我快到了,但离它还有几个像素,无法达到完美的触感。有没有更好的方法,或者也许我的放置精度可以得到完美的对齐?

    cell = [tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [self tableviewCellWithReuseIdentifier:CellIdentifier];
    }
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(0.0f, 5.0f, 320.0f, 44.0f)];
    
    [button setTitle:@"Do Stuff" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(doStuff:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button];
    
    9 回复  |  直到 13 年前
        1
  •  13
  •   Ryan Townshend    16 年前

    您可以在didSelectRowAtIndexPath:方法中伪造它。 让tableView单元格充当按钮。

    [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES];
    [self doStuff];
    [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
    

    使用 setSelected: animated: 假装按下按钮并使其褪色。 也许能帮你。

        2
  •  3
  •   DongSu Cha DongSu Cha    16 年前

    类(UITableViewCell)具有contentView属性。 所以我把基本按钮放在UITableViewCell变量的contentView中。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ・・・
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(230.0f, 4.0f, 60.0f, 36.0f);
        [btn setTitle:@"OK" forState:UIControlStateNormal];
        [btn setTitle:@"OK" forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(onClickRename:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];
        ・・・
        return cell;
    }
    

    对不起,我的英语不好。

        3
  •  3
  •   Matteo Alessani Cord LaBarre    13 年前

    如果你想让它完美匹配,只需将单元格的边界称为CGRect。这样做:

    button.frame = cell.bounds;
    

    这应该会让你的按钮和你的手机完全一样。希望这能有所帮助!

        4
  •  0
  •   Chris Lundie    16 年前

    这可能无关紧要,但您的代码可能存在问题,每次重复使用单元格时,都会向单元格中添加另一个按钮。

        5
  •  0
  •   Geraud.ch    16 年前

    我和你有同样的问题。Ryan给出了答案:使用didSelectedRowAtIndexPath:方法将单元格本身用作按钮。

        6
  •  0
  •   drewh    16 年前

    您还可以向UITableViewCell添加UIImage,使单元格看起来像一个按钮。

        7
  •  0
  •   Dave Batton    16 年前

    您还可以创建一个带有按钮的视图,并将其放置在其上方部分的页脚中。这样,您就不必担心背景中的单元格图像。我这样做是为了在分组表视图中并排放置两个半角按钮。

        8
  •  0
  •   eric eric    16 年前

    FWW,我把按钮放在CGRectMake的应用程序中(9,0302,45) 我觉得它看起来很完美。

        9
  •  0
  •   Taras Kalapun    15 年前
    UIButton *btnView = [[UIButton alloc] init];
    btnView.frame = CGRectMake(52.0f, 2.0f, 215.0f, 38.0f);
    [btnView setImage:[UIImage imageNamed:@"invite.png"] forState:UIControlStateNormal];
    [btnView addTarget:self action:@selector(showInviteByMail) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btnView];
    [btnView release];