繁体   English   中英

UITableViewCell 中的自定义按钮

[英]A custom Button in a UITableViewCell

我有一个三段式的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];

如果你想把它放在 header 部分(而不是一个单元格),你可以:

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

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

    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];

此示例将如下所示:

在此处输入图像描述

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

暂无
暂无

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

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