2012-09-24 99 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; 
相關問題