如何在表视图中维护uibutton的状态

时间:2012-12-27 09:12:25

标签: objective-c uitableview ios5

我有一个表格视图,我使用的是custom cell。我在每个表视图单元格上有两个按钮,名为“edit”,“cancel”都有cellForRow中的图像。我想要的是,当用户同时点击编辑按钮时,同一行取消按钮应该更改其图像。代码正在工作,但它没有更改相同的行取消按钮图像。它改变了另一行取消按钮图像。如何保持每个按钮的状态。

这是代码

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  simpleTableIdentifier = @"MenuNameCell";
  MenuNameCell *cell = (MenuNameCell *)[tableView  dequeueReusableCellWithIdentifier:simpleTableIdentifier];

     if (cell== nil) {
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuNameCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
    NSLog(@"---------new cell agin");   
}
   else
       [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];


// Creating Label Menu Name
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.text =  [_hotel._orderedMenus objectAtIndex:indexPath.row];

// Creating Label Menu Cost
_amountMenu   = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
_amountMenu.backgroundColor = [UIColor clearColor];
_amountMenu.text  = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];

// Creating Text Field For Order Quantity
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];

//Creating Button For CANCEL Order
_cancelButton  = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
[_cancelButton setTag:indexPath.row];
[_cancelButton addTarget:self  action:@selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];

// Creating Button For Check Order
_checkButton  = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setTag:indexPath.row];
[_checkButton setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];

// Adding All To Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];

[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];

//objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );

return cell;
 }

-(void)editQuantity:(id)sender{

NSLog(@"count of the array is----%d",[imageViewArray count]);
button = (UIButton *)sender;
row = button.tag;
NSLog(@"---rowww%d",row);
_textFieldQuantity.userInteractionEnabled = YES;
UIImage *buttonImage = [UIImage imageNamed:@"edit_over.png"];

[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageCancel = [UIImage imageNamed:@"check.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
    _cancelButton.tag =  0;
}

-(void)cancelOreder:(id)sender{
button = (UIButton *)sender;
row = button.tag;
NSLog(@"The Row Selected iS At Cancel Order ISSSS----%d", row);
if (_cancelButton.tag ==  0){
    _textFieldQuantity.userInteractionEnabled = NO;
    UIImage *buttonImageCancel = [UIImage imageNamed:@"check_over.png"];
    [_cancelButton setBackgroundImage:buttonImageCancel  forState:UIControlStateNormal];
    UIImage *buttonImageCancel1 = [UIImage imageNamed:@"cancel.png"];
    [_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
    UIImage *buttonImage = [UIImage imageNamed:@"edit.png"];
    [_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
    _cancelButton.tag = 1;
}
else
{
    UIAlertView *alert =  [[UIAlertView alloc] initWithTitle:@"iHomeDelivery" message:@"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
  }
}

3 个答案:

答案 0 :(得分:0)

尝试在同一个单元格中使用不同的标记制作_cancelButton和_checkButton,我认为将两个标记放在同一行中是很困惑的。

答案 1 :(得分:0)

首先为每个按钮添加不同的标记,如:

[_checkButton setTag:indexPath.row];

[_cancelButton setTag:indexPath.row+100];

和您的editQuantity:方法

button = (UIButton *)sender;
row = button.tag;

NSIndexPath *currentIndexPath = [NSIndexPath indexPathForRow:row inSection:0];

UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:currentIndexPath];

newCancelButton = (UIButton *)[newCell.contentView viewWithTag:row+100];

[newCancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];

cancelOreder:方法中执行相同操作以更改_checkButton的图像。

另外,使用它来删除所有子视图,

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

答案 2 :(得分:0)

请尝试此解决方案

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL_ID"];
  if(cell == nil)
  {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL_ID"];

    UIButton *_cancelButton  = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_cancelButton setFrame:CGRectMake(65, 13, 100, 28)];
    [_cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
    [_cancelButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [_cancelButton addTarget:self  action:@selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
    [_cancelButton setTag:1];

    UIButton *_checkButton  = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_checkButton setFrame:CGRectMake(200, 13, 100, 28)];
    [_checkButton setTitle:@"Check" forState:UIControlStateNormal];
    [_checkButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
    [_checkButton setTag:2];

    [cell.contentView addSubview:_checkButton];
    [cell.contentView addSubview:_cancelButton];
  }
  return cell;
}

-(void) cancelOreder:(id)sender
{
  UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
  UIButton *btn = (UIButton *)[cell viewWithTag:2];
  if(btn != nil)
  {
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  }
}

-(void) editQuantity:(id)sender
{
  UITableViewCell *cell = (UITableViewCell *)[(UIButton *)sender superview];
  UIButton *btn = (UIButton *)[cell viewWithTag:1];
  if(btn != nil)
  {
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  }
}

在这个例子中我改变标签的文字颜色你可以改变图像,希望这会对你有帮助