【问题标题】:How to add a clickable button on table cell?如何在表格单元格上添加可点击按钮?
【发布时间】:2012-09-15 13:45:21
【问题描述】:

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

这是我的 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];}}

MessageTable.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;
}

请帮帮我。 谢谢。

【问题讨论】:

  • 每个视图上都有这个按钮吗?渲染是否正确?
  • 是的,如果条件为真,那么按钮将显示在视图中。按钮显示正确,但我无法在点击时调用方法。
  • 您可能还应该显示用于创建表格单元格的代码。此外,当您单击按钮单元格时会发生什么?有没有崩溃?还是完全没有效果?
  • 你能把这段代码放在你的问题中吗?
  • 为什么要像附件一样添加按钮?你应该将你的按钮作为一个简单的子视图直接添加到你的单元格中,比如[cell addSubview:chat_pic_btn];...你已经尝试过这种方式了吗?

标签: iphone ios xcode uitableview uibutton


【解决方案1】:

我建议您删除 TableViewCell 中的 Button 插座。只需在 cellForRowAtIndexPath 中动态创建按钮: 我创建了一个 SampleCell 类,它是 UITableViewCell 的子类,它有一个名为“lbl”的 UILabel 插座。 这应该适用于您的代码,假设您的选择器与您放置 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;
}

【讨论】:

  • hmm 奇怪.. 我前一阵子编码了.. 你删除了 TableViewCell 类中的 IBOutlet UIButton *btn 吗?
  • 使用 cell.bounds.origin,而不是 cell.frame.origin 作为按钮的框架。
【解决方案2】:

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

self.userInteractionEnabled = YES;

【讨论】:

    【解决方案3】:
    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;
    

    【讨论】:

    • 抱歉,是我发帖时的错误。但它不起作用......而且你提供的代码行也不起作用。这是我调用的方法 -(IBAction) show_chat_pic:(id)sender { NSLog(@"in show"); UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 50, 310, 320)]; imageview.image=sender_profile2; [self.view addSubview:imageview]; }
    【解决方案4】:
    【解决方案5】:

    你没有给按钮任何控制事件或将它从UIControlStateNormal更改为UIControlEventTouchUpInside,并且只执行一次:)

    【讨论】:

    • 我想我已经给了事件 [cell.chat_pic_btn addTarget:self action:@selector(show_chat_pic:) forControlEvents:UIControlEventTouchUpInside];
    • 然后检查此链接并尝试修改您的代码stackoverflow.com/questions/10732229/…
    猜你喜欢
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多