我有一個簡單的截面表視圖。我在圖像左側添加了一個按鈕。此按鈕是可選的。每次只能選擇一個按鈕(每個單元格具有同一類型的單個按鈕),並且所選按鈕背景圖像會變爲特定選定圖像,其他按鈕背景圖像更改以取消選擇圖像。如果再次點擊所選圖像,它將保持選定狀態。 我怎麼沒有得到正確的行爲。 代碼:一次設置一個選定/未選擇的圖像一個在分段表中的可用視圖單元
- (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];
}
我在做什麼錯, 任何建議
改變這個[BTN了setBackgroundImage:[UIImage的imageNamed:@ 「cellLeftImageEnabled.png」] forState:UIControlStateSelected]。 –