我想在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];
}
它有效但有一个例外 - 所有坐标都是针对原始单元格位置计算的(即,即使用户向下滚动,单元格原点也保持不变,因此屏幕上不应该变暗的部分可能会被错误地放置。我怎样才能修改代码,以便考虑所选单元格的实际坐标?
答案 0 :(得分:0)
使用Nick对Get UITableviewCell position from "visible" area or window的回答我能够获得所选单元格的当前位置。
新实施:
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;