2012-01-31 25 views
0

如何在UITableView中選擇單元格後不再選擇或選擇其他單元格時消除創建的視圖。iOS:UITableView澄清

假設我有下面的didSelectRowAtIndexPath方法的代碼:

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

    CGSize cellSize = [tableView cellForRowAtIndexPath:indexPath].frame.size; 

    UIView *selectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, (int)cellSize.width, (int)(cellSize.height + 100))]; 
    selectionView.layer.borderWidth = 2; 

    [[tableView cellForRowAtIndexPath:indexPath]addSubview:selectionView]; 

} 

現在,我想,當我專注於另一種細胞再次刪除創建selectionView和創建它到我聚焦單元。該問題是當我第一次選擇一個單元格時,它完美地工作,但是當我選擇另一個單元格時,從前一個單元格創建的selectionView仍然不會消失,並且它已經複製了該視圖。我想如何解決這個問題?需要建議.. :(感謝..

回答

1

您需要添加tag對於selectionView爲也遵循

selectionView.tag = 100; 

,你需要通過聲明NSIndexPath類成員和保持它有最後選定indexPath的參考。

所以在選擇新的細胞,以獲得最後選定indexpath細胞,並從細胞中刪除視圖如下

UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastSelIndexPath]; 
UIView *view = [lastCell viewWithTag:100]; 
[view removeFromSuperview]; 
+0

感謝aadhira ......嘿嘿,我們有相同的猜測它工作..:D – Aldee 2012-01-31 04:47:36

1

你真的需要尋找到子類UITableViewCell,如果你想自定義UI的選擇,然後覆蓋[UITableViewCell setSelected:animated:](或[UITableViewCell setHighlighted:animated:])來執行你的定製。

+0

謝謝ELL ... – Aldee 2012-01-31 03:07:17