2012-05-24 38 views
5

có ma thuật đen tối này trong ios đang ngăn nút của tôi được nhấp. nếu tôi không thêm nút vào uitableviewcell, và tôi nhấp vào nút, sự kiện được kích hoạt. nhưng nếu nút nằm trong uitableviewcell, nó sẽ không được kích hoạt, có vẻ như bảng tôi có mã mẫu sẵn sàng, nếu các bạn có thể giúp tôi, hãy tạo một ứng dụng xem đơn giản trong xcode và chỉ dán đangUIButton không phản hồi sự kiện nhấp chuột trong UITableViewCell

//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 

xin vui lòng giúp tôi cảm thấy trường hợp này cần được phổ biến trong ios, nhưng không ai có giải pháp fo r này ???

chỉnh sửa: khi nhấp chuột vào nút nó in NSLog msg trong xcode console ... do đó hãy chắc chắn rằng ... ur nhìn vào kết quả đó ...

+0

Chỉ là đề xuất nhưng tại sao không tạo nút trong trình tạo giao diện và ánh xạ IBAction cho sự kiện touchUpInside? Có thể làm cho công việc dễ dàng hơn. Ngoài ra, bạn đã thử sử dụng trình nhận dạng cử chỉ để thay thế? Tôi đoán rằng hộp chứa lần đầu tiên nhận được sự kiện chạm và không chuyển nó đến các bản xem phụ của nó vì có lẽ cờ tương tác người dùng của nó bị tắt. Xin lỗi Im không phải với mac của tôi ngay nhưng hy vọng những gợi ý giúp! – shawndreck

Trả lời

7

bạn chưa đưa ra bất kỳ kiểm soát sự kiện vào nút .Change nó để UIControlEventTouchUpInside từ UIControlStateNormal

7

Thay vì sử dụng UIControlStateNormal cho ControlEvents sử dụng UIControlEventTouchUpInside như thế này

[btn addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside]; 
+0

cảm ơn ... vì đã trả lời đó là vấn đề. tôi sẽ chấp nhận cả hai câu trả lời nếu tôi có thể, tôi bỏ phiếu trả lời ur là tốt. – user1118019

1

Hãy thử làm điều này:

[btn.titleLabel setUserInteractionEnabled: NO];

Các vấn đề liên quan