[英]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.