简体   繁体   English

UITableViewCell 中的自定义按钮

[英]A custom Button in a UITableViewCell

I have a UITableView with three section.我有一个三段式的UITableView I have a custom UITableViewCell .我有一个自定义UITableViewCell I want to put a button in the first section of my table view to handle an action.我想在我的表格视图的第一部分放置一个按钮来处理一个动作。

How to can do this please?请问如何才能做到这一点?

just create the button programmatically只需以编程方式创建按钮

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

If you want to put it in the section header (rather than a cell), you could:如果你想把它放在 header 部分(而不是一个单元格),你可以:

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

You can also put the button directly into a UITableViewCell which is located in your first section.您还可以将按钮直接放入位于第一部分的UITableViewCell中。 In your cellForRowAtIndexPath you can use for example:在您的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];

This example will look like this:此示例将如下所示:

在此处输入图像描述

and will be places directly in the cell.并将直接放置在单元格中。

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

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