2014-01-23 52 views
0

我有一個簡單的截面表視圖。我在圖像左側添加了一個按鈕。此按鈕是可選的。每次只能選擇一個按鈕(每個單元格具有同一類型的單個按鈕),並且所選按鈕背景圖像會變爲特定選定圖像,其他按鈕背景圖像更改以取消選擇圖像。如果再次點擊所選圖像,它將保持選定狀態。 我怎麼沒有得到正確的行爲。 代碼:一次設置一個選定/未選擇的圖像一個在分段表中的可用視圖單元

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
// UITableViewCell *cell=nil; 
CustomCellMasterCustomer *cell=nil; 

static NSString *[email protected]"Locations"; 
Customer *mCustomer; 
cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if(cell==nil){ 
//  cell=[self reusableContentWithIdentifier:cellIdentifier]; 
    cell=(CustomCellMasterCustomer *)[self reusableContentWithIdentifier:cellIdentifier]; 

} 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.tag = indexPath; 
[button setBackgroundImage:[UIImage imageNamed:@"cellLeftImageDisabled.png"] forState:UIControlStateNormal]; 
[button setFrame:CGRectMake(10, 5, 36, 30)]; 
[button addTarget:self action:@selector(viewDetail:) forControlEvents:UIControlEventTouchUpInside]; 
[cell.contentView addSubview:button]; 

cell.aCustomer = mCustomer;//alok added 

if (indexPath == self.mPreviousIndex) 
{ 
    [button setBackgroundImage:[UIImage imageNamed:@"cellLeftImageEnabled.png"] forState:UIControlStateNormal]; 
} 
else 
{ 
    [button setBackgroundImage:[UIImage imageNamed:@"cellLeftImageDisabled.png"] forState:UIControlStateNormal]; 

} 
return cell; 

}

-(void)viewDetail:(id)sender 
{ 
CGPoint buttonOrigin = [sender frame].origin; 
// this converts the coordinate system of the origin from the button's superview to the table view's coordinate system. 
CGPoint originInTableView = [self.allCustomerTable convertPoint:buttonOrigin fromView:[sender superview]]; 

          // gets the row corresponding to the converted point 
          NSIndexPath *rowIndexPath = [self.allCustomerTable indexPathForRowAtPoint:originInTableView]; 


CustomCellMasterCustomer *cell = (CustomCellMasterCustomer *)[self.allCustomerTable cellForRowAtIndexPath:rowIndexPath]; 

[self fillDetailViewWithCustomerInformation:cell.aCustomer]; 

//change button background image 


UIButton *btn = (UIButton *)sender; 

[btn setBackgroundImage:[UIImage imageNamed:@"cellLeftImageEnabled.png"] forState:UIControlStateNormal]; 
self.mPreviousIndex = rowIndexPath; 
[self.allCustomerTable reloadData]; 

}

我在做什麼錯, 任何建議

+0

改變這個[BTN了setBackgroundImage:[UIImage的imageNamed:@ 「cellLeftImageEnabled.png」] forState:UIControlStateSelected]。 –

回答

0

試試這個。由於indexpath始終使用相同的行和段號重新創建。它不會和以前一樣。

if (indexPath.row == self.mPreviousIndex.row && indexPath.section == self.mPreviousIndex.section) 
{ 
    [button setBackgroundImage:[UIImage imageNamed:@"cellLeftImageEnabled.png"] forState:UIControlStateNormal]; 
} 

而且這兩行代碼,不需要再

/* UIButton *btn = (UIButton *)sender; 

    [btn setBackgroundImage:[UIImage imageNamed:@"cellLeftImageEnabled.png"] forState:UIControlStateNormal];*/ 
+0

這是行不通的,0行0行爲默認情況下,我的意思是默認情況下圖像被選中,任何建議爲什麼?我認爲self.mprevindex創建問題 – Alok

+0

if(indexPath.row == self.mPreviousIndex.row && indexPath.section == self.mPreviousIndex.section && self.mPreviousIndex!= nil) – Alok

+0

@Alok你試過這個嗎? – Mani

0

你不應該添加一個按鈕出方的if塊。在UITableView單元格將被重用,如果單元格被重用,並且已經有一個按鈕添加到contentView那麼你不應該再添加一個。因此請創建一個自定義單元格,並通過在customCell.h中聲明buttion屬性使該類實例在類的外部可見。現在檢查的情況 -

if (selected) 

{ 

    cell.button.backgroundImage = selectedImage; 

} 

else 

{ 

    cell.button.backgroundImage = unselectedImage; 

} 
相關問題