0

Button doesn't respond in 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 doesn't call after press button. Does anyone have some idea why and some solution?

I think button even not pressed only cell become active.

joran
  • 169,992
  • 32
  • 429
  • 468
UnRewa
  • 2,462
  • 2
  • 29
  • 31

2 Answers2

0

Your deleteButtonUr button's frame(width) exceeds the rowView's frame.so increase the view's frame as like,

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

otherwise adjust the label and deleteButton's frame with in your rowView's frame

EXC_BAD_ACCESS
  • 2,699
  • 2
  • 21
  • 25
  • Tnx my mistake)) now i can press button... but selector doesnt call – UnRewa Oct 13 '11 at 12:47
  • -(void) removeCellAtIndexPath:(NSString*)text – UnRewa Oct 13 '11 at 12:50
  • U cant send string as parameter i think – EXC_BAD_ACCESS Oct 13 '11 at 12:53
  • no I create -(void)deleteButtonClick:(id)sender but this dont call too – UnRewa Oct 13 '11 at 13:06
  • Do like this.It will definitely work. [deleteButton addTarget: self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside]; – EXC_BAD_ACCESS Oct 13 '11 at 13:07
  • ))) i am noob in programming but i change selector in target – UnRewa Oct 13 '11 at 13:12
  • Oops!!! u r spking abt pickerViewCell.I tht of tableViewCell.Am not sure abt pickerView Dude.till now I just added label only in pickerView.Am not sure abt button click dude ;) – EXC_BAD_ACCESS Oct 13 '11 at 13:22
  • ok i try UIButton* deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; deleteButton.frame = CGRectMake(210,10,60,30.0); [deleteButton setTitle:@"delete" forState:UIControlStateNormal]; [deleteButton addTarget: self action:@selector(deleteButtonClick:) forControlEvents:UIControlEventTouchUpInside]; – UnRewa Oct 13 '11 at 13:27
  • and nothink i thin its imposible – UnRewa Oct 13 '11 at 13:27
0

I find ways. just in

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

Create custom View with button

Like this

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

}

And use:

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

Or:

 -(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];
     }
}
UnRewa
  • 2,462
  • 2
  • 29
  • 31