2015-06-26 113 views
1

我試圖在按下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]; 
} 

回答

2

的主要問題可能是您的cellForRowAtIndexPath:方法。 UITableView單元在屏幕上顯示時被重新使用。 dequeueReusableCellWithIdentifier:方法返回一個單元格,如果它已被標記爲可以重新使用。你一定已經看到了在cellForRowAtIndexPath:方法中使用UITableView的這種方法。 (見link

所以在cellForRowAtIndexPath:,你將不得不配置每個單元格,因爲它正在加載或否則它會顯示舊值(因爲單元格正在重新使用)。

您可以聲明屬性或NSIndexPath類型的簡單變量。請將該變量稱爲_selectedIndexPath。然後在didSelectRowAtIndexPath:中,您可以將此屬性分配給所選的indexPath。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSArray *indexPaths = nil; 
    if (_selectedIndexPath) { 
     indexPaths = @[_selectedIndexPath, indexPath]; 
    } else { 
     indexPaths = @[indexPath]; 

    } 
    _selectedIndexPath = indexPath; 
    [tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
} 

- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Your Cell Identifier"]; 
    UIButton *addNotesButton = (UIButton *)[cell viewWithTag:106]; 

    if (indexPath.row == _selectedIndexPath.row) { 
     [addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
    } else { 
     [addNotesButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 
    } 
} 
+0

嗨,謝謝你的回覆。我有一個問題,其中indexPath.row == _selectedIndexPath.row始終等於將我的按鈕始終設置爲藍色。 – Mich

+0

它仍然改變所有按鈕的顏色,而不是我按下的按鈕。 – Mich

+0

@Mich:你能提供你使用的代碼嗎?在看不到代碼的情況下,很難說出可能會出現什麼問題。 – tek3

1

您不需要在索引路徑的選擇行中手動更改顏色。只需設置UIControlStateSelected的顏色,然後在按鈕點擊的操作中將按鈕選擇屬性設置爲YES。從你的代碼我認爲這應該工作。

內的索引路徑方法

[addNotesButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected]; 

和按鈕操作方法

-(IBAction)addNotes:(id)sender 
{ 
    UIButton *button = (UIButton*)sender; 
    buttons.selected = !button.isSelected; 
} 

細胞爲行,我認爲這會工作。

+0

這產生了與我原來的代碼相同的結果。當我只希望我選擇的按鈕改變顏色時,所有按鈕都會改變顏色。 – Mich

0

嘗試一切後失敗。我最終在每一行中都有一個隱藏的值,當按下按鈕時它會改變。所以代碼讀取值然後配置每行的按鈕。