2012-09-24 97 views
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];}} 

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

认罪我帮助我。 谢谢。

+0

是每次鉴于此按钮?它是否正确渲染? – ThomasW

+0

如果条件为真,那么Ya有条件,那么按钮将显示在视图上。按钮显示正确,但我无法通过点击调用方法。 – Saurabh

+0

您应该也可以显示您用于创建表格单元格的代码。另外,当你点击按钮单元格时会发生什么?有没有崩溃?或者根本没有效果? – ThomasW

回答

10

我建议你在你的TableViewCell中删除你的Button插座。并在cellForRowAtIndexPath中动态创建按钮: 我创建了一个SampleCell Class,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; 
} 
+2

感谢您的答复。但它仍然不起作用。 :( – Saurabh

+0

嗯奇怪..我编码它前一阵子..你删除自己的IBOutlet中的UIButton *干训局在TableViewCell类? –

+0

使用cell.bounds.origin,不cell.frame.origin该按钮的框架。 –

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; 
+0

对不起,我发布的问题是我的错。但它不工作...和你提供的代码行也不起作用。这是我调用的方法 - (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]; } – Saurabh

+2

不是答案....它应该是评论... – Maulik

+0

CHK更新回答@ user1652551 – Rajneesh071

0

您还没有给出任何控制事件的按钮或从UIControlStateNormal更改为UIControlEventTouchUpInside,做它只有一次:)

+0

我想我已经给了事件[cell.chat_pic_btn addTarget:self action:@selector(show_chat_pic :) forControlEvents:UIControlEventTouchUpInside]; – Saurabh

+0

然后检查此链接并尝试修改您的代码http:// stackoverflow。COM /问题/ 10732229/UIButton的 - 不回应到点击事件中-的UITableViewCell – IronManGill

5

一定要设置用户交互为YES细胞观点和你的按钮

self.userInteractionEnabled = YES; 
相关问题