2012-12-06 41 views
0

我有一個表格視圖,其中每個單元格包含一個按鈕。我不使用didSelectRowAtIndexPath委託,但我的自定義方法的按鈕操作。我希望點擊每個單元格內的按鈕時,該單元格應該突出顯示或更改顏色,並在單擊某個其他單元格中的按鈕時使其恢復到正常狀態(顏色)。(使該單元格突出顯示)。我的按鈕操作方法是:可用視圖單元格高亮

- (void)SelectButtonTapped:(UIButton *)button 
{ 


    self.tabBarController.tabBar.userInteractionEnabled = YES; 

    AppDelegate *proDel = [[UIApplication sharedApplication]delegate]; 

    UITableViewCell *cell = (UITableViewCell *)button.superview.superview; 

    //MyCustomCell *cell = (MyCustomCell *)button.superview.superview; 

    NSIndexPath *indexPath = [homeTable indexPathForCell:cell]; 


    //cell.backgroundColor = [UIColor lightGrayColor]; 


    //[homeTable setNeedsDisplayInRect:[homeTable rectForRowAtIndexPath:indexPath]]; 


    DetailViewController *objDetail=[[DetailViewController alloc] init]; 

    Home *tempSearchObj=(Home *)[profileArray objectAtIndex:indexPath.row]; 
    objDetail.firstName=tempSearchObj.userName; 


    objDetail.userImageUrl=tempSearchObj.imageUrl; 

    objDetail.passedProfileID = tempSearchObj.profileID; 

    plsSelectLabel.text = [NSString stringWithFormat:@"%@",objDetail.firstName]; 

    proDel.globalName = plsSelectLabel.text; 

    proDel.globalProfileId = objDetail.passedProfileID; 


} 

但是,這似乎並不奏效。任何幫助,將不勝感激!!

回答

1

設置小區選擇樣式的cellForRowAtIndexPath爲:

if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 
    } 

和:

yourButton.tag = indexPath.row; 

添加下面一行在你的方法來突出細胞:

UIButton *clickButton = (UIButton *)sender; 

int addButtonIndex = clickButton.tag; 

NSIndexPath *indexPathHighlight = [NSIndexPath indexPathForRow:addButtonIndex inSection:0]; 

UITableViewCell *newCell = [yourTable cellForRowAtIndexPath:indexPathHighlight]; 

[newCell setSelected:YES animated:YES]; 

取消選擇以前cell:

NSIndexPath *previousIndexPathHighlighted = [NSIndexPath indexPathForRow:previousTag inSection:0]; 

UITableViewCell *previousCell = [yourTable cellForRowAtIndexPath:previousIndexPathHighlighted]; 

[previousCell setSelected:NO animated:YES]; 

previousTag = clickButton.tag; 
+0

這將使單元格在單元格上單擊時突出顯示,但我希望單擊該單元格的子視圖的按鈕 – user1845209

+0

@ user1845209如果將標記添加到您的單元格中,可以在方法中創建單元格的引用按鈕作爲indexPath.row添加按鈕時。 –

+0

@ user1845209我已更新我的答案。現在試試.. –

0

正確的做法是,在您的cellForRowAtIndex:方法中,您可以瞭解您的單元格是否需要突出顯示。然後,點擊一個按鈕,並配置任何你需要做的之後,你應該簡單地做

NSArray *indexes = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]]; 
[self.tableView reloadRowsAtIndexPaths:indexes withRowAnimation:UITableViewRowAnimationNone]; 

另一種方式去了解這一點,是接入小區,並直接改變它。但不是button.superview.superview,而是

UITableViewCell *cell = [self.tableView cellForRowAtIndex:[NSIndexPath indexPathForRow:0 inSection:1]]; 
+0

Bu我的錶行是動態的,它們是從nsmutableArray加載的,並且此數組可以在Web服務發生更改時隨時擴展或縮小,因此indexPathForRow:0不是我想去的方式 – user1845209

+0

那是隻是一個例子......你應該能夠在點擊一個按鈕後知道你在哪個單元格上 – Ismael

0

我覺得做的最好的方式,就是讓你的按鈕的

NSInteger selectedCellIndex; 

設置標籤的cellForRowAtIndex方法

button.tag = indexPath.row 

設置selectedCel LINDEX當您單擊按鈕,並刷新表

selectedCellIndex = button.tag; 
[self.tableView reloadData]; 

然後在cellForRowAtIndex方法檢查,如果該指數是一樣的selectedCellIndex和添加不同顏色的單元格。

if(indexPath.row == selectedCellIndex){ 
    //set different colors 
} 

請記住在if(cell == nil)檢查之外執行此操作,否則它將無法工作。

相關問題