【问题标题】:uitableview cell highlightuitableview 单元格突出显示
【发布时间】:2012-12-06 11:38:09
【问题描述】:

我有一个表格视图,其中每个单元格都包含一个按钮。我不使用 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;


}

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

【问题讨论】:

    标签: objective-c ios xcode uitableview uibutton


    【解决方案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;
    

    【讨论】:

    • 这将使单元格在单击单元格时突出显示,但我希望单击作为单元格子视图的按钮
    • @user1845209 如果在添加按钮时将标签作为 indexPath.row 添加到按钮,则可以在方法中创建单元格的引用。
    • 当我单击每个单元格的按钮时,上面的代码使所有单元格都被选中。我希望当我单击任何单元格上的按钮时,只有那个单元格应该被突出显示,而之前选择的单元格应该进入正常的未高亮状态
    • @user1845209 为此创建一个循环并在其中创建表中所有单元格的引用并使其未选中。或者存储上一个按钮标签并仅在您的方法中取消选择该单元格。
    【解决方案2】:

    解决此问题的正确方法是,在您的 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]];
    

    【讨论】:

    • 但我的表格行是动态的,它们是从 nsmutableArray 加载的,并且该数组可以在 Web 服务更改时随时扩展或缩小,所以我认为 indexPathForRow:0 不是要走的路
    • 这只是一个例子……点击按钮后你应该能够知道你在哪个单元格
    【解决方案3】:

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

    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) 检查之外执行此操作,否则将无法正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2016-02-06
      相关资源
      最近更新 更多