我有自定义表视图单元格我有两个UIButton和一个文本字段命名为编辑和取消和一个textField。当我单击编辑同时启用TextFeild Interaction时,取消按钮图像应该更改。它的工作对我来说很好!!
但当我点击编辑按钮时另一个单元格取消bitton图像ID自动更改!我知道发生了这种情况,因为我正在重复使用这个细胞!但我无法找到解决方案......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
simpleTableIdentifier = @"MenuNameCell";
cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (!cell) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(@"---------new cell agin");
}
else
{
for (UIView *view in [cell.contentView subviews])
[view removeFromSuperview];
NSLog(@"---------older use");
// _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 setBackgroundImage:[UIImage imageNamed:@"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:@selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
[_checkButton setTag:indexPath.row];
// 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 addTarget:self action:@selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
[_cancelButton setTag:indexPath.row];
// Adding All To Call Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
return cell;
}
答案 0 :(得分:0)
老兄,我不认为这是因为重复使用单元格,如果你认为那么对我有利,只需保留所有代码;但是,您必须检查点击事件中的任何内容,这会改变您的身份。
另外一件事我们习惯使用标签来达到这个目的,但是我们手里拿着一些由苹果公司提供的东西,用于协会这样的事情。在这里,您应该尝试: -
*的 objc_setAssociatedObject * 强>
您始终可以参考您的单元格或您进行关联的任何内容。 只是谷歌,乍一看我只是粘贴下面的代码检查它:(但你需要根据你的要求工作)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView != popTbl)
{
NSString *CellIdentifier = @"CellIdentifier";
cell = (CustomizeConditionVC *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomizeConditionVC" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.lbl.text = [objArr objectAtIndex:indexPath.row];
[cell.lbl setBackgroundColor:[UIColor clearColor]];
cell.lbl.font = [UIFont fontWithName:@"Arial" size:(17.0)];
cell.lbl.font = [UIFont boldSystemFontOfSize:17];
cell.lbl.textColor = [UIColor colorWithRed:0.12 green:0.27 blue:0.34 alpha:1.0];
cell.lbl.autoresizingMask = NO;
NSString* lblStringLen = NULL;
lblStringLen = [objArr objectAtIndex:0];
unsigned int kFactor = -1;
kFactor = cell.lbl.frame.size.width/[lblStringLen length];
CGRect rect = cell.lbl.frame;
rect.size.width = kFactor * [[objArr objectAtIndex:indexPath.row] length];
[cell.lbl setFrame:rect];
[cell.txtField setBackgroundColor:[UIColor clearColor]];
cell.txtField.font = [UIFont fontWithName:@"Arial" size:(17.0)];
cell.txtField.textColor = [UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.0];
CGRect rectTxtField = [cell.txtField frame];
rectTxtField.size.height = 27;
[cell.txtField setFrame:rectTxtField];
[cell.txtField setDelegate:self];
[cell.txtField setTag:indexPath.row];
[cell.txtView setBackgroundColor:[UIColor clearColor]];
[cell.txtView setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1.0]];
cell.txtView.font = [UIFont fontWithName:@"Arial" size:(17.0)];
cell.txtView.textColor = [UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.btn1 setTag:1];
[cell.btn2 setTag:2];
[cell.btn3 setTag:3];
[cell.btn1 addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn2 addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
[cell.btn3 addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnClearence addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnMaintenance addTarget:self action:@selector(ButtonAtCellControl:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnClearence setHidden:YES];
[cell.btnMaintenance setHidden:YES];
// //////////////
objCell = cell;
// //////////////
int getVersionFromCategory = [[self ExtractTheFirstCharBeforePeriodOfDevice] intValue];
// cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];
switch (indexPath.row)
{
case 2:
case 4:
cell.txtView = [UITextView SetTheBorderProperties:cell.txtView];
[cell.txtView setHidden:NO];
[cell.txtView setDelegate:self];
[cell.txtField setHidden:YES];
[cell.smallBtn setHidden:YES];
[cell.largeBtn setHidden:YES];
[cell.txtView setFrame:CGRectMake(cell.lbl.frame.origin.x+13, cell.lbl.frame.origin.y + cell.lbl.frame.size.height + 3, 295-39, (121-(cell.lbl.frame.origin.y + cell.lbl.frame.size.height +3)+89))];
[cell.btn1 setHidden:YES];
[cell.btn2 setHidden:YES];
[cell.btn3 setHidden:YES];
if(indexPath.row == 4)
{
[cell.txtView setHidden:YES];
[cell.btnClearence setHidden:NO];
[cell.btnMaintenance setHidden:NO];
}
break;
case 3:
[cell.btn1 setHidden:NO];
prototype1 = btn1 = cell.btn1;
/////////New Entry on Date:Nov.07...
prototype2 = cell.btn2;
prototype3 = cell.btn3;
////////
[cell.btn2 setHidden:NO];
[cell.btn3 setHidden:NO];
[cell.btn1 setFrame:CGRectMake(cell.txtField.frame.origin.x, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
[cell.btn2 setFrame:CGRectMake(5+cell.txtField.frame.origin.x+cell.txtField.frame.size.width/3+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
[cell.btn3 setFrame:CGRectMake(15 + cell.txtField.frame.origin.x+2*(cell.txtField.frame.size.width/3)+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
[cell.txtField setHidden:YES];
[cell.smallBtn setHidden:YES];
[cell.largeBtn setHidden:YES];
// UIAlertView *alert = [[UIAlertView alloc]
// initWithTitle:@"Hover display.." message:[NSString stringWithFormat:@"%d",cell.btn1.frame.size.width] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
// [alert release];
break;
default:
[cell.btn1 setHidden:YES];
[cell.btn2 setHidden:YES];
[cell.btn3 setHidden:YES];
[cell.txtView setHidden:YES];
[cell.txtView setDelegate:nil];
[cell.smallBtn addTarget:self action:@selector(DepictingPopUp:) forControlEvents:UIControlEventTouchUpInside];
[cell.largeBtn addTarget:self action:@selector(DepictingPopUp:) forControlEvents:UIControlEventTouchUpInside];
objc_setAssociatedObject(cell.smallBtn,SMALL_BTN_REF,indexPath,OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(cell.largeBtn,SMALL_BTN_REF_1,indexPath,OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(cell.largeBtn, LARGE_BTN_REF, cell.txtField, OBJC_ASSOCIATION_RETAIN);
objc_setAssociatedObject(cell.smallBtn, CELL_TO_CELL_REF, cell.txtField, OBJC_ASSOCIATION_RETAIN);
break;
}
// int getVersionFromCategory = [[self ExtractTheFirstCharBeforePeriodOfDevice] intValue];
// if(getVersionFromCategory > 3)
// {
// cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
// cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
// cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
// }
// cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
// cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
// cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
if([[NSUserDefaults standardUserDefaults] boolForKey:@"PreviewCase"])
{
switch (indexPath.row)
{
case 0:
cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"qty"];
break;
case 1:
//cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"extraCol"];
cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"condition"];
break;
case 2:
cell.txtField.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"furtherNote"];
cell.txtView.text = [(NSMutableDictionary*)getTheConditionListVC objectForKey:@"furtherNote"];
break;
case 3://liability//+(UIButton*)MButton3DLookForSelected:(UIButton*)_rectForButton AndButtonTitle:(NSString*)_btnTitle
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Hover display.." message:[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"liability"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
//[alert show];
[alert release];
if(getVersionFromCategory > 3)
{
switch ([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"liability"] intValue]) {
case 1:
//case 0:
cell.btn1 = [UIButton MButton3DLookForSelected:cell.btn1 AndButtonTitle:@"gdf"];
cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
break;
case 2:
cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
cell.btn2 = [UIButton MButton3DLookForSelected:cell.btn2 AndButtonTitle:@"gdf"];
cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
break;
case 3:
cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
cell.btn3 = [UIButton MButton3DLookForSelected:cell.btn3 AndButtonTitle:@"gdf"];
break;
default:
break;
}
}
// else
// {
// switch ([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"liability"] intValue]) {
// case 1:
// //case 0:
// cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];
// cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn2.frame];
// cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn3.frame];
// break;
// case 2:
// cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];
// cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn2.frame];
// cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn3.frame];
// break;
// case 3:
// cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn1.frame];
// cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn2.frame];
// cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:cell.btn3.frame];
// break;
// default:
// break;
// }
// }
}
break;
case 4:
if([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"extraCol"] intValue] == 4)
{
//////////////
objCell = cell;
//////////////
[[(CustomizeConditionVC*)objCell btnClearence] setImage:[UIImage imageNamed:@"brush_selected.png"] forState:UIControlStateNormal];
[[(CustomizeConditionVC*)objCell btnMaintenance] setImage:[UIImage imageNamed:@"cross.png"] forState:UIControlStateNormal];
}
else
if([[(NSMutableDictionary*)getTheConditionListVC objectForKey:@"extraCol"] intValue] == 5)
{
//////////////
objCell = cell;
//////////////
[[(CustomizeConditionVC*)objCell btnClearence] setImage:[UIImage imageNamed:@"brush.png"] forState:UIControlStateNormal];
[[(CustomizeConditionVC*)objCell btnMaintenance] setImage:[UIImage imageNamed:@"cross_selected.png"] forState:UIControlStateNormal];
}
break;
default:
break;
}
}
else {
//int getVersionFromCategory = [[self ExtractTheFirstCharBeforePeriodOfDevice] intValue];
if(getVersionFromCategory > 3)
{
cell.btn1 = [UIButton MButton3DLook:cell.btn1 AndButtonTitle:@"gdf"];
cell.btn2 = [UIButton MButton3DLook:cell.btn2 AndButtonTitle:@"gdf"];
cell.btn3 = [UIButton MButton3DLook:cell.btn3 AndButtonTitle:@"gdf"];
}
else
{
// [cell.btn1 setFrame:CGRectMake(cell.txtField.frame.origin.x, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
// [cell.btn2 setFrame:CGRectMake(5+cell.txtField.frame.origin.x+cell.txtField.frame.size.width/3+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
// [cell.btn3 setFrame:CGRectMake(15 + cell.txtField.frame.origin.x+2*(cell.txtField.frame.size.width/3)+5, cell.txtField.frame.origin.y, cell.txtField.frame.size.width/3, cell.txtField.frame.size.height)];
// [cell.txtField setHidden:YES];
// [cell.smallBtn setHidden:YES];
// [cell.largeBtn setHidden:YES];
//MCustomizeButtonRect
/*
cell.btn1 = [[MCustomizeButtonRect alloc] initWithFrame:prototype1.frame];
cell.btn2 = [[MCustomizeButtonRect alloc] initWithFrame:prototype2.frame];
cell.btn3 = [[MCustomizeButtonRect alloc] initWithFrame:prototype3.frame];
*/
// UIAlertView *alert = [[UIAlertView alloc]
// initWithTitle:@"Hover display.." message:[NSString stringWithFormat:@"%d",cell.btn1.frame.size.width] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
// [alert release];
}
}
// prototype1 = cell.btn1;
// prototype2 = cell.btn2;
// prototype3 = cell.btn3;
return cell;
}
else
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(50,50, 201, 47)
reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = @"N/A";
NSString* dropMenusString = NULL;
dropMenusString = [[tempOfArr objectAtIndex:indexPath.row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([dropMenusString length] > 0)
cell.textLabel.text = dropMenusString;
//cell.textLabel.text = [[tempOfArr objectAtIndex:indexPath.row] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
return cell;
}
}
Now, access your association as :-
-(void)DepictingPopUp:(id)sender
{
[popTbl isHidden]?[popTbl setHidden:NO]:[popTbl setHidden:YES];
[popTbl setHidden:NO];
NSIndexPath* iPath = nil;
iPath = objc_getAssociatedObject(sender, SMALL_BTN_REF);
NSIndexPath* iPathNew = nil;
iPathNew = objc_getAssociatedObject(sender, SMALL_BTN_REF_1);
(iPath)?(associateIndex = iPath.row):(associateIndex = iPathNew.row);
UITextField* tempField = nil;
tempField = objc_getAssociatedObject(sender, LARGE_BTN_REF);
UITextField* tempMField = nil;
tempMField = objc_getAssociatedObject(sender, CELL_TO_CELL_REF);
if(tempField)
{
tempTxtField = tempField;
tempMField = nil;
}
else
{
tempTxtField = tempMField;
tempField = nil;
}
[popTbl setFrame:[(NatolliAppDelegate*)[[UIApplication sharedApplication] delegate] GetTheCordinateFromTextFieldDynamically:tempTxtField WithButton:sender isAssociativeRefAvailable:YES IfAvailableThenIndex:associateIndex]];
[popTbl isHidden]?[popTbl setHidden:NO]:[popTbl setHidden:YES];
[popTbl setHidden:NO];
switch (iPath.row || iPathNew.row)
{
case 0:
tempOfArr = quantArr;
[popTbl reloadData];
break;
case 1:
tempOfArr = conditionArr;
[popTbl reloadData];
break;
default:
break;
}
}
请注意,由于我的忙碌,我只需要粘贴我的一个代码,因此您需要按照您的要求工作。
如有任何疑虑,请回复我。
答案 1 :(得分:0)
使用以下代码直接创建新单元格而不重用旧单元格。
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
而不是
cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];