我試圖在按下tableviewCell時按下按鈕的顏色。然而,我的代碼改變了表中每個按鈕的顏色,而不僅僅是我選擇的單元格中的一個,在UITableviewCell中更改按鈕顏色
我該如何去改變我按下的按鈕的顏色。
請參閱下面我的代碼,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];
[addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Test";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106];/
[addNotesButton addTarget:self action:@selector(addNotes :) forControlEvents:UIControlEventTouchUpInside];
}
嗨,謝謝你的回覆。我有一個問題,其中indexPath.row == _selectedIndexPath.row始終等於將我的按鈕始終設置爲藍色。 – Mich
它仍然改變所有按鈕的顏色,而不是我按下的按鈕。 – Mich
@Mich:你能提供你使用的代碼嗎?在看不到代碼的情況下,很難說出可能會出現什麼問題。 – tek3