表格视图按钮索引单元格
Posted
技术标签:
【中文标题】表格视图按钮索引单元格【英文标题】:Table view button index cell 【发布时间】:2012-12-26 06:04:51 【问题描述】:我在该单元格中有一个表格视图是自定义的。我在表格视图的每个单元格上添加两个按钮。当我同时单击第一个按钮时,来自同一单元格的第二个按钮正在更改其图像。为此,我有editQuantity
和Cancelorder
等方法。使用@sel。我遇到一个问题,当我单击第一个按钮更改同一个单元格另一个按钮时,它更改另一个单元格按钮也是当我滚动表格查看它的损失所有选定按钮时
Here Is My Code--
- (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
NSArray *arrayView = [cell.contentView subviews];
for (UIView *vTemp in arrayView)
[vTemp removeFromSuperview];
NSLog(@"---No New Cell hiiii");
// Setting Tag To UIButton
_checkButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
_cancelButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
// 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 Check Order
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setTag:indexPath.row];
_checkButton.titleLabel.tag = indexPath.row;
[_checkButton setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
// 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.titleLabel.tag = indexPath.row;
[_cancelButton addTarget:self action:@selector(cancelOreder:) 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
button = (UIButton *)sender;
row = button.tag;
col = button.titleLabel.tag;
NSLog(@"Check Button index is %d",row);
NSLog(@"cehck title is %d",col);
_textFieldQuantity.userInteractionEnabled = YES;
UIImage *buttonImage = [UIImage imageNamed:@"edit_over.png"];
[_checkButton 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];
【问题讨论】:
不会阅读所有内容,请将您的代码缩减为仅对您的问题有必要的内容。 我已经编辑了我的代码请提出建议 【参考方案1】:问题是任何控件的默认标记值都设置为0
,而您使用了_cancelButton.tag = 0;
。只需将值更改为10
,然后将_cancelButton.tag = 1;
更改为1
为11
。
这会解决你的问题:)
【讨论】:
在您的-(void)editQuantity:(id)sender
和 -(void)cancelOreder:(id)sender
方法中更改标签值。 NEVER USE 0
当你想在条件语句中使用它时。【参考方案2】:
正如我所见,_checkButton、_nameLabel、_amountMenu、_textFieldQuantity 和 _cancelButton 都是您的视图控制器类的实例变量。
在 cellForRowAtIndexPath: 中,当您为所有这些分配新对象时,它们将引用显示的最后一个单元格的对象。意味着所有这些都将指向最后一个单元格的组件。 因此,更改 _checkButton 和 _cancelButton 的背景图像将影响最后一个单元格中的按钮。
同样在 cellForRowAtIndexPath: 创建这些按钮时,您正在为它们设置背景图像,因此在滚动后您的按钮图像会发生变化。请记住 cellForRowAtIndexPath: 在滚动时隐藏后,每次该行变为可见时都会调用该行。
您需要在单独的数组中维护按钮的状态,以便在出列后重置 cellForRowAtIndexPath: 中的按钮状态。
【讨论】:
你的解释是正确的,但是如何保持按钮状态可以给我示例代码 我现在什么都没有。有时间我会尽量给你做一个。以上是关于表格视图按钮索引单元格的主要内容,如果未能解决你的问题,请参考以下文章
使用 insertRowsAtIndexPaths 时的表视图单元格索引