2011-10-13 71 views
0

按钮在viewForRow不响应:UIPickerView的UIButton在viewForRow不响应

UIView * rowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)] autorelease]; 

UIButton* deleteButton = [[[UIButton alloc]init]autorelease]; 
deleteButton.frame = CGRectMake(200, 10, 20,20); 
[deleteButton setImage:[UIImage imageNamed:@"IconTrash.png"] forState: UIControlStateNormal]; 
[deleteButton addTarget: self action:@selector(removeCellAtIndexPath:) forControlEvents: UIControlEventTouchUpInside]; 

UILabel *labelText = [[UILabel alloc]init]; 
labelText.text = [NSString stringWithFormat:@"%@",[[localFlagNotes objectAtIndex:row] value]]; 
labelText.font = [UIFont fontWithName:@"Arial" size: 24]; 
labelText.frame = CGRectMake(0, 10, 200, 28); 
labelText.backgroundColor = [UIColor clearColor]; 

[rowView addSubview:labelText]; 
[rowView addSubview:deleteButton]; 
[labelText release]; 
return rowView; 

removeCellAtIndexPath按下按钮后不会调用。有人有一些想法,为什么和一些解决方案?

我觉得按钮甚至没有按下只有细胞变得活跃。

回答

0

我想办法。刚刚在

- (UIView*)pickerView:(UIPickerView *)pickersView viewForRow:(NSInteger)row 
      forComponent:(NSInteger)component reusingView:(UIView *)view 
    { 
      PickerCustomView * rowView = [[PickerCustomView alloc]initWithFrame:CGRectMake(5, 0, 310, 50) withText:[[localFlagNotes objectAtIndex:row] value]] ; 
      rowView.delegate = self; 
      return rowView; 
    } 

创建按钮定制视图

像这样

- (id)initWithFrame:(CGRect)frame withText:(NSString*)text 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
{ 
    deleteButton = [[UIButton alloc]init]; 

    [deleteButton setImage:[UIImage imageNamed:@"IconTrash"] forState: UIControlStateNormal]; 
    [deleteButton addTarget:self action:@selector(deleteButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    deleteButton.frame = CGRectMake(250, 10, 35,35); 

    textLabel = [[UILabel alloc]init]; 
    originText = text; 
    NSString *trimmed = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    textLabel.text = trimmed; 
    textLabel.font = [UIFont fontWithName:@"Arial" size: 24]; 
    textLabel.frame = CGRectMake(20, 10, 230, 28); 
    textLabel.backgroundColor = [UIColor clearColor]; 

    [self addSubview:textLabel]; 
    [self addSubview:deleteButton];  

} 
return self; 

}

及用途:

-(void)deleteButtonAction:(id)sender 
{ 
    [delegate removeCellAtIndexPath:originText]; 
} 

或者:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    if ((location.x > 10 && location.x < 38) && (location.y > 240 && location.y < 265) ) 
    { 
     [self deleteButtonAction:nil]; 
    } 
} 
+0

它不适合我 –

0

你deleteButtonUr按钮的边框(宽度)超过rowView的frame.so增加视图的框架像,

UIView * rowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)] autorelease]; 

以其他方式调整标签和deleteButton的框架在rowView的框架

+0

TNX我的错)),现在我可以按下按钮......但选择犯规调用 – UnRewa

+0

如何ü定义乌尔选择 –

+0

不喜欢, - (空)removeCellAtIndexPath:(ID)发送 {//我们的代码 } –