2013-12-11 17 views
1

我正在爲每個單擊的行添加按鈕子視圖。我試圖刪除每次按鈕subView一旦我點擊不同的行,並添加按鈕子視圖到另一行我點擊。如何使用標記實用地刪除按鈕

哪裏會是我的問題?它將subView添加到每行而不刪除它們中的任何一行。

if (sender.tag == 212) 
    { 
    myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    //Debug shows that every-time myButton&myButton2 hasn't tag?? 

    if (myButton2.tag != 0 && myButton.tag != 0) 
    { 
     [myButton removeFromSuperview]; 
     [myButton2 removeFromSuperview]; 
    } 

    UITableViewCell *cell = (id)sender.superview.superview.superview; 

    CGPoint center= sender.center; 
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:commonTable.table]; 
    NSIndexPath *indexPath1 = [commonTable.table indexPathForRowAtPoint:rootViewPoint]; 

    itemdata1 = [itemList objectAtIndex:indexPath1.row]; 

    if (closeManager) [closeManager release]; 

    CGFloat __y = commonTable.y + commonTable.table.y - commonTable.table.contentOffset.y + cell.height*indexPath1.row + sender.y; 
    CGFloat __x = commonTable.x + commonTable.table.x + sender.x; 
    CGRect layerBox1 = CGRectMake(618, __y, 90, sender.height+3); 
    CGRect layerBox2 = CGRectMake(888, __y, 94, sender.height+3); 

    NSLog(@"%f and %f", __x, __y); 

    myButton.frame = layerBox1; 
    [[myButton layer] setBorderWidth:2.0f]; 
    myButton.layer.borderColor = [UIColor redColor].CGColor; 
    [myButton addTarget:self action:@selector(func1:) forControlEvents:UIControlEventTouchUpInside]; 

    myButton2.frame = layerBox2; 
    [[myButton2 layer] setBorderWidth:2.0f]; 
    [myButton2 addTarget:self action:@selector(func2:) forControlEvents:UIControlEventTouchUpInside]; 
    myButton2.layer.borderColor = [UIColor redColor].CGColor; 

    [self addSubview:myButton]; 
    [self addSubview:myButton2]; 

    myButton.tag = myButton2.tag = 666; 

} 

編輯的Bhumeshwer katre:

if (myButton&&myButton2) 
    { 
     [myButton removeFromSuperview]; 
     [myButton2 removeFromSuperview]; 
    } 

    //Other variables 

    if(!myButton){ 
     myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     myButton.frame = layerBox1; 
     [[myButton layer] setBorderWidth:2.0f]; 
     myButton.layer.borderColor = [UIColor redColor].CGColor; 
     [myButton addTarget:self action:@selector(func1:) forControlEvents:UIControlEventTouchUpInside]; 
     myButton.tag = 666; 
     [self addSubview:myButton]; 
    } 
+0

你究竟想要什麼?你面臨的問題是什麼? –

+0

@Bhumeshwerkatre我在點擊的行上添加子視圖。我只想在我點擊其他行時刪除該子視圖。 –

回答

2

不要每次創建按鈕,這樣

在這裏,你創建的UIButton

myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

的每一次新的實例,你在做這個

//Debug shows that every-time myButton&myButton2 hasn't tag?? 
     if (myButton2.tag != 0 && myButton.tag != 0) 
     { 
      [myButton removeFromSuperview]; 
      [myButton2 removeFromSuperview]; 
     } 
    //It will just remove new instance of UIButton and no more your UIButton will be there on UIView. 

嘗試這樣

if(!myButton){ 
     myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    } 
    if(!myButton2) { 
     myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    } 

//Don't remove your button just change frame 

如果你仍然想刪除按鈕,然後先刪除

[myButton removeFromSuperview]; 
    [myButton2 removeFromSuperview]; 

//then create new instance of button 
+0

現在我已經嘗試過'if(!myButton)',並且我已經刪除了'removeFromSuperview'。它在不打印,如果我點擊其他行。 –

+0

在哪裏你正在添加UIButton。嘗試調試代碼,看看UIButton對象有什麼問題。 –

+0

我已經編輯了我的代碼,請你檢查一下嗎? –

0

如果您使用的tableview細胞,同時創造tableViewCell使用此下列情況下,

1)設置標籤按鈕。

2)隱藏通過cell.yourButton.hidden = YES

3)按鈕,當你點擊一行,只是記下indexPath爲self.selectedIndexPath = indexPathdidSelectRowAtIndexPath:

4)現在刷新你的tableView。只需在cellForRowAtIndexpath:中添加一個條件如下。

if (indexPath.row == self.selectedIndexPath.row) 
{ 
    cell.yourButton.hidden = NO; 
} 
else 
    cell.yourButton.hidden = YES; 
+0

我沒有使用'cell' :( –

+0

Mani如果有50多個單元,內存會增加。請好一些好的解決方案 –

+0

@RavirajJadeja內存如何增加甚至10,000個單元?只有可見的單元留在內存中tableViewCell堆棧。這裏沒有什麼是錯誤的記憶。如果發現任何錯誤,請詳細解釋。這將有助於訪問.. – Mani

1

嘗試添加標記按鈕之前把它們添加到子視圖。

所以這個:

[self addSubview:myButton]; 
[self addSubview:myButton2]; 

myButton.tag = myButton2.tag = 666; 

應該變成這個樣子:

myButton.tag = myButton2.tag = 666; 

[self addSubview:myButton]; 
[self addSubview:myButton2]; 

問候,

hris.to

0
for (UIView *view in self.view.subView) { // instead of self.view you can use your main view 
     if ([view isKindOfClass:[UIButton class]] && view.tag == ?) { 
      UIButton *btn = (UIButton *)view; 
      [btn removeFromSuperview]; 
     } 
    } 
0

我認爲你必須先設置標記你的按鈕,因爲每個UIControl的默認標記是0;