簡體   English   中英

iOS:UITableView說明

[英]iOS: UITableView clarifications

當不再選擇單元格或選擇其他單元格時,如何消除在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仍然不會消失,並且它已經復制了視圖。 我怎么想解決這個問題? 需要建議.. :(謝謝..

如果你想要一個自定義選擇UI,你真的需要研究子類化UITableViewCell 然后覆蓋[UITableViewCell setSelected:animated:] (或[UITableViewCell setHighlighted:animated:] )來執行自定義。

您需要為selectionView添加tag ,如下所示

selectionView.tag = 100;

此外,您需要通過聲明NSIndexPath類成員並保留它來獲取最后選擇的indexPath的引用。

因此,在選擇新單元格時,獲取具有最后選定索引路徑的單元格,並從單元格中刪除視圖,如下所示

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM