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;


}

但这似乎不起作用。任何帮助,将不胜感激!!

4

3 回答 3

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];

取消选择上一个单元格:

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

UITableViewCell *previousCell = [yourTable cellForRowAtIndexPath:previousIndexPathHighlighted];

[previousCell setSelected:NO animated:YES];

previousTag = clickButton.tag;
于 2012-12-06T11:45:22.777 回答
0

我认为最好的方法是制作一个

NSInteger selectedCellIndex;

在 cellForRowAtIndex 方法中设置按钮的标签

button.tag = indexPath.row

单击按钮并重新加载表格时设置 selectedCellIndex

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

然后在 cellForRowAtIndex 方法中检查索引是否与 selectedCellIndex 相同,并为单元格添加不同的颜色。

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

请记住在 if( cell == nil ) 检查之外执行此操作,否则它将不起作用。

于 2012-12-06T16:33:56.573 回答
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]];
于 2012-12-06T11:45:49.633 回答