【问题标题】:how to maintain state of uibutton in table view如何在表格视图中维护uibutton的状态
【发布时间】:2012-12-27 09:12:24
【问题描述】:

我有一个表格视图,我使用的是custom cell。我在每个名为“编辑”和“取消”的表格视图单元格上有两个按钮,它们在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];
  }
}

【问题讨论】:

  • 为什么不将按钮放在自定义单元格中,让单元格管理状态。那么只需将按下的按钮委托给您的视图控制器?
  • 您的解决方案很好。但是当我试图做这个应用程序在滚动时崩溃
  • 失败点在哪里?你得到什么错误?您可以发布使用带有按钮的单元格时的代码吗?
  • 我通过anushs方法得到了解决方案
  • 如果@anusha 的回答对你有帮助,那么就接受她的回答,这样以后其他人就能看到真正的答案

标签: objective-c uitableview ios5


【解决方案1】:

尝试让_cancelButton和_checkButton在同一个单元格中使用不同的标签,我认为让它们在同一行中使用相同的标签是个难题。

【讨论】:

  • @JigneshKalantri 对,但是“+100”可能会导致另一个问题,超过 100 个单元格时会再次出现问题。
  • 是的,我明白我给了 1000。我知道它不好。将尝试另一种方法
  • 如果您有任何其他选择,请让 mekno
【解决方案2】:

首先为每个按钮赋予不同的标签,例如:

[_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)];

【讨论】:

  • 当我再次向下滚动表格视图时,默认图像应用于按钮,也给我解决方案
  • @JigneshKalantri 您是否按照我的回答更改了删除单元格子视图的代码??
  • @JigneshKalantri 滚动时,每个单元格将重新加载,图像将更改,如 cellForRowAtIndexPath: 方法。要解决这个问题,您必须在方法内部检查..
  • @JigneshKalantri ...用于将编辑后的单元格 indexPath.row 存储在一个数组和您的 cellForRowAtIndexPath 中:检查该数组中的当前行是否设置图像
  • 我在每个行单元格中放置了一个我想要的另一件事,如果用户选择第三个单元格编辑按钮同时启用第三个单元格 textfeild 用户交互,我们该怎么做?
【解决方案3】:

请尝试此解决方案

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多