5

iOS にこの暗い魔法があり、ボタンがクリックされないようにしています。uitableviewcell にボタンを追加せずにボタンをクリックすると、イベントがトリガーされます。しかし、ボタンが uitableviewcell にある場合、トリガーされません。サンプル コードの準備ができているようです。皆さんが私を助けることができる場合は、xcode で単純なシングルビュー アプリケーションを作成し、次のコードを貼り付けてください。

//GRDViewController.h

#import <UIKit/UIKit.h>

@interface GRDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView* container;
@property (nonatomic, strong) UIButton* button;
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSArray* arr;

@end

//GRDViewController.m

#import "GRDViewController.h"

@implementation GRDViewController
@synthesize button, container, arr, tableView;

- (void)_btnTapped {
NSLog(@"TAPPED");
}

- (void)viewDidLoad 
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.button addTarget:self action:@selector(_btnTapped)    forControlEvents:UIControlEventTouchUpInside];
   [self.button setTitle:@"CLICK ME" forState:UIControlStateNormal];
  self.arr = [[NSArray alloc] initWithObjects:@"123", @"456", @"678", nil];

   self.container = [[UIView alloc]initWithFrame:self.button.frame];
  [self.container addSubview:self.button];
  [self.view addSubview:self.container];
  //[self.view  addSubview:self.button];

  self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, 400, 400)];

 self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight |   UIViewAutoresizingFlexibleWidth;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  self.tableView.delegate = self;
  self.tableView.dataSource = self;

  [self.view addSubview:self.tableView];

self.tableView.backgroundColor  = [UIColor redColor];





}



   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"coolcell";
UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:cellIdentifier];

  if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:cellIdentifier];
      //cell.accessoryType = UITableViewCellAccessoryNone;

      UIButton * btn =  [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
         [btn addTarget:self action:@selector(_btnTapped)   forControlEvents:UIControlStateNormal];
      [btn setTitle:[self.arr objectAtIndex:[indexPath row]]     forState:UIControlStateNormal];
     [cell.contentView addSubview:btn];
         }

    return cell; 
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
   return [self.arr count];      
}
 @end

このケースはiosでは一般的だと思いますが、誰もこれに対する解決策を持っていませんか???

編集: ボタンをクリックすると、xcode コンソールに NSLOG msg が出力されます...そのため、結果を確認してください...

4

3 に答える 3

7

ボタンに制御イベントを与えていません。からに変更しUIControlEventTouchUpInsideますUIControlStateNormal

于 2012-05-24T06:33:41.027 に答える
7

UIControlStateNormalControlEventsに使用する代わりに、UIControlEventTouchUpInsideこのように使用します

[btn addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside];
于 2012-05-24T06:38:06.803 に答える