在我的應用程序中,我面臨一個非常奇怪的問題。我試圖解決它幾個小時,但無法找到到目前爲止的問題。iphone開發:更改tableviewcell上的按鈕圖像
那麼,我有一個自定義單元格的tableview。每個單元格我都有一個按鈕。它就像一個空盒子。按下按鈕時,我想更改該按鈕的相關單元格圖像。其實我成功地做到了這一點,但不知何故,其他一些單元格的按鈕圖像也在變化,我無法弄清楚原因。這裏是我的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//create new cell
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.tickButton setTag:indexPath.row];
[cell.tickButton addTarget:self action:@selector(buttonWasPressed:)forControlEvents:UIControlEventTouchUpInside];
//cell has 2 label so I filled them with the contents of my object array that part is working with no problem
cell.leftLabel.text = [NSString stringWithFormat:@"%@", [[contacts objectAtIndex:indexPath.row]name]];
cell.rightLabel.text = [NSString stringWithFormat:@"%@", [[contacts objectAtIndex:indexPath.row]email]];
return cell;
}
在我buttonWasPressed方法,我只是做:
-(IBAction)buttonWasPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
[button setImage:[UIImage imageNamed:@"ticck.jpeg"] forState:UIControlStateNormal];
}
正如我所說的它的工作原理之前,還會改變其他一些按鈕圖像,並且還當我向下滾動,回到我看到一些按鈕圖像的初始位置發生了變化。
很好的答案。我是一隻新的蜜蜂。請大家粘貼一些代碼片段。它會有幫助。謝謝 –