ios 如何在表格单元格上添加可点击的按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12559277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a clickable button on table cell?
提问by Saurabh
I am adding a button in table view cell dynamically. Button shown on table but I am not able to click on them. Here is my Code,
我正在动态地在表格视图单元格中添加一个按钮。按钮显示在桌子上,但我无法点击它们。这是我的代码,
This my tableviewcell class code:
这是我的 tableviewcell 类代码:
MessageTableViewCell.h
MessageTableViewCell.h
#import <UIKit/UIKit.h>
@interface MessageTableViewCell : UITableViewCell {
{IBOutlet UIButton *chat_pic_btn;}}
@property (nonatomic, retain) UIButton *chat_pic_btn;
@end;
MessageTableViewCell.m
MessageTableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {chat_pic_btn=[[UIButton alloc]init];
[self.contentView addSubview:chat_pic_btn];}}
MessageTable.m
消息表.m
-(void)customActionPressed :(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Custom Button Pressed"
message:[NSString stringWithFormat: @"You pressed the custom button on cell"]
delegate:self cancelButtonTitle:@"Great"
otherButtonTitles:nil];
[alertView show];
}
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellIdentifier";
MessageTableViewCell *cell = (MessageTableViewCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// cell = [[MessageTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
cell = [[MessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.chat_pic_btn.frame = CGRectMake(180, 24, 70,35);
[cell.chat_pic_btn setImage:[UIImage imageNamed:@"done.png"]forState:UIControlStateNormal];
[cell.chat_pic_btn addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchDown];
return cell;
}
Please help me out. Thanks.
请帮帮我。谢谢。
回答by Jenn Eve
I suggest you delete your Button outlet in your TableViewCell. and just create your button dynamically in cellForRowAtIndexPath: I created a SampleCell Class, subclass of UITableViewCell, it has a UILabel outlet i called "lbl." This should work on your code, assuming that your selector is on the same class where you put your tablview;
我建议您删除 TableViewCell 中的 Button 插座。只需在 cellForRowAtIndexPath 中动态创建您的按钮:我创建了一个 SampleCell 类,UITableViewCell 的子类,它有一个 UILabel 出口,我称为“lbl”。这应该适用于您的代码,假设您的选择器位于您放置 tablview 的同一个类中;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"SampleCell";
SampleCell *cell = (SampleCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SampleCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (SampleCell *)currentObject;
break;
}
}
}
// Configure the cell.
cell.lbl.text = @"Hello";
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(cell.frame.origin.x + 100, cell.frame.origin.y + 20, 100, 30);
[button setTitle:@"World" forState:UIControlStateNormal];
[button addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:button];
return cell;
}
回答by nilveryboring
Be sure to Set UserInteraction to YES in cell view and your button
一定要设置用户交互为YES细胞观点和你的按钮
self.userInteractionEnabled = YES;
回答by Rajneesh071
UIButton *yourBtn = [[UIButton alloc] initWithFrame:CGRectMake(0,0, 50, 50)];
[yourBtn setImage:[UIImage imageNamed:@"yourImage.png"] forState:UIControlStateNormal];
[yourBtn addTarget:self action:@selector(submitBtnPress:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = yourBtn;
回答by SachinVsSachin
回答by IronManGill
You have not given any control event to the button or Change it to UIControlEventTouchUpInside
from UIControlStateNormal
and do it only once :)
您没有为按钮提供任何控制事件或将其更改为UIControlEventTouchUpInside
fromUIControlStateNormal
并且只执行一次:)