2012-08-29 50 views
1

我想在uitableview中的所有未選定單元格上添加透明圖層。 我實現下面的代碼:lightbox在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

使用Nick的答案Get UITableviewCell position from "visible" area or window我能夠獲得當前位置選中的單元格

新的i mplementation:

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; 
相關問題