5

我在表格视图单元格中动态添加一个按钮。按钮显示在桌子上,但我无法点击它们。这是我的代码,

这是我的 tableviewcell 类代码:

MessageTableViewCell.h

#import <UIKit/UIKit.h>
@interface MessageTableViewCell : UITableViewCell {
{IBOutlet UIButton *chat_pic_btn;}}
@property (nonatomic, retain) UIButton *chat_pic_btn;
@end;

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

消息表.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;
}

请帮帮我。谢谢。

4

5 回答 5

10

我建议您删除 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;
}
于 2012-09-24T07:28:28.013 回答
5

确保在单元格视图和按钮中将 UserInteraction 设置为 YES

self.userInteractionEnabled = YES;
于 2013-07-10T21:14:35.507 回答
1

http://www.roostersoftstudios.com/2011/05/01/iphone-custom-button-within-a-uitableviewcell/

这可能会帮助你

于 2012-09-24T06:01:06.130 回答
1
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;
于 2012-09-24T05:30:28.707 回答
0

您没有为按钮提供任何控制事件或将其更改为UIControlEventTouchUpInsidefromUIControlStateNormal并且只执行一次:)

于 2012-09-24T05:31:48.067 回答