1

我想在 uitableview 中的所有未选择的单元格上方添加透明层。我实现了以下代码:

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

    customCell *selectedCell = (customCell*)[tableView cellForRowAtIndexPath:indexPath];

    overlayAboveSelectedCell = [[UIView alloc] initWithFrame:CGRectMake(listViewTable.frameOrigin.x, listViewTable.frame.origin.y, listViewTable.frameWidth, selectedCell.frameOrigin.y)];
    overlayAboveSelectedCell.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [self.view insertSubview:overlayAboveSelectedCell aboveSubview:listViewTable];

    overlayBellowSelectedCell = [[UIView alloc] initWithFrame:CGRectMake(listViewTable.frameOrigin.x, selectedCell.frameOrigin.y+selectedCell.frameHeight, listViewTable.frameWidth, listViewTable.frameHeight-selectedCell.frameOrigin.y-selectedCell.frameHeight)];
    overlayBellowSelectedCell.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    [self.view insertSubview:overlayBellowSelectedCell aboveSubview:listViewTable];

    listViewTable.scrollEnabled = NO;

    [selectedCell doSomething];
}

它工作正常,有一个例外 - 所有坐标都是针对原始单元格位置计算的(即,即使用户向下滚动,单元格原点也保持不变,因此不应该变暗的屏幕部分可能放置错误。怎么可能我修改代码以便考虑所选单元格的实际坐标?

4

1 回答 1

0

使用尼克对从“可见”区域或窗口获取 UITableviewCell 位置的回答,我能够获得所选单元格的当前位置。

新实现:

customCell *selectedCell = (customCell*)[tableView cellForRowAtIndexPath:indexPath];
CGRect position = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];

overlayAboveSelectedCell = [[UIView alloc] initWithFrame:CGRectMake(listViewTable.frame.origin.x, listViewTable.frame.origin.y, listViewTable.frameWidth, position.origin.y)];
overlayAboveSelectedCell.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[self.view insertSubview:overlayAboveSelectedCell aboveSubview:listViewTable];

overlayBellowSelectedCell = [[UIView alloc] initWithFrame:CGRectMake(listViewTable.frameOrigin.x, position.origin.y+selectedCell.frameHeight, listViewTable.frameWidth, listViewTable.frameHeight-position.origin.y-selectedCell.frameHeight)];
overlayBellowSelectedCell.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
[self.view insertSubview:overlayBellowSelectedCell aboveSubview:listViewTable];

listViewTable.scrollEnabled = NO;
于 2012-08-29T13:33:51.947 回答