2012-12-06 44 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)检查之外执行此操作,否则它将无法工作。

相关问题