2012-12-23 106 views
0

我有一個表格視圖和兩個按鈕,每個單元格中名爲廣告編輯和取消。當用戶點擊編輯同時同一行取消按鈕圖像應該改變。代碼工作正常,但當我點擊第一個單元格編輯按鈕取消按鈕的第三個單元格圖像正在改變insted第一個單元格。我應該爲此做些什麼?所以它會改變同一個單元格中心按鈕的圖像。表格視圖單元格中的按鈕不響應其索引路徑

這是我的代碼!!

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

simpleTableIdentifier = @"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; 
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:row] stringValue]; 

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; 

} - (空)cancelOreder:(ID)發送{

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

}

+0

請添加編碼數量: –

+0

@rahul - 在代碼中添加editQuanty方法請檢查它並告訴我該怎麼辦 –

+0

您正在使用實例變量_cancelButton更改取消按鈕的圖像。你確定它指向取消同一單元格的按鈕嗎?在editQuantity:方法中,您需要通過搜索標籤與編輯按鈕的標籤相同的按鈕來從同一單元格中獲取取消按鈕。然後改變它的形象。 –

回答

0

在editQuantity,它看起來像你正在改變_checkButton和_cancelButton的背景圖像,但是您沒有通過指定具體實例紅色標籤。

您提到第三行上的取消按鈕正在改變。這是添加到表格中的最後一行嗎?

EDITED 在editQuantity方法中嘗試類似這樣的操作;

UIView *buttonToEdit; 
buttonToEdit = [_cancelButton viewWithTag:row]; 
[buttonToEdit setBackgroundImage:buttonImageCancel forState:UIControlStateNormal]; 

我還沒有測試過,但在這種情況下,buttonToEdit應該指向你點擊的行中的按鈕。因此,您將在同一行中的按鈕上設置背景圖像。

希望它適合你。

+0

@T Davey沒有它的最後一個單元格...當我點擊任何單元格編輯按鈕時,隨機更改其他單元格單元格取消按鈕 –

相關問題