Iphone UITableViewCell中的自定义按钮

Iphone UITableViewCell中的自定义按钮,iphone,Iphone,我有一个带有三个部分的UITableView。我有一个自定义的UITableViewCell。我想在表视图的第一部分中放置一个按钮来处理操作 如何才能做到这一点,请?只需以编程方式创建按钮即可 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button addTarget:self action:@selector(aMethod:) forControlEvents:UIC

我有一个带有三个部分的
UITableView
。我有一个自定义的
UITableViewCell
。我想在表视图的第一部分中放置一个按钮来处理操作


如何才能做到这一点,请?

只需以编程方式创建按钮即可

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

如果要将其放在节标题(而不是单元格)中,可以:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section==0)
    {
        // create button and return it
    }
}

您还可以将按钮直接放入位于第一个部分的
UITableViewCell
。在
单元格中,您可以使用例如:

    UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [yourButton setTitle:@"Click me" forState:UIControlStateNormal];
    [yourButton setFrame: CGRectMake( 20.0f, 3.0f, 280.0f, 33.0f)];
    [yourButton addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];

    [cell addSubview:yourButton];
此示例如下所示:


并且将直接放置在单元格中。

显然,此方法不仅需要创建按钮,还需要创建保存该按钮的视图。我希望你不介意,但我试着用简短的介绍来澄清你的答案。