2012-02-28 41 views
0
  • 我想iPhone:隱藏在UITableViewCell中的附件按鈕的基礎上正確故事板

    什麼有一個tableview.I只是想通過點擊它在TableViewCell躲UIButtonTypeContactAdd附件。

  • 我的問題

    當我拍了拍附件按鈕A(我只在整個過程中抽頭),它正確牆根。但是當我向下滾動桌面時,我發現另一個附件按鈕B可笑地隱藏起來。滾動快速到tableview中的頂側之後,按鈕B傢伙在那裏再次,同時另一個按鈕C牆根...

    很可惜我不能把圖像在我post.Hope你能理解發生了。

  • 代碼
    的tableView:的cellForRowAtIndexPath:

    static NSString *CellIdentifier = @"All Name Showing Table"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    if(!cell.accessoryView){ 
        UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
        cell.accessoryView = button; 
    } 
    

- (IBAction)buttonTapped:(UIButton *)sender 
    { 
     UITableViewCell *tvc = (UITableViewCell *)[sender superview]; 
     NSString *peopleTapped = [NSString stringWithFormat:@"you have favored %@",tvc.textLabel.text]; 
     NSLog(@"%@",peopleTapped); 

     sender.hidden = YES; 
    } 

是所有這一切都因爲電池再利用的機制呢?

對不起,我英文很差。
謝謝!

回答

1

您不能以這種方式使用表格。您應該有一個存儲按鈕附件按鈕狀態的對象的數據模型。

static NSString *CellIdentifier = @"All Name Showing Table"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
if(!cell.accessoryView){ 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; 
    cell.accessoryView = button; 
} 

Model *model = [_array objectAtIndex:indexPath.row]; 
button.tag = [_array indexOfObject:indexPath.row]; 
button.hidden = model.hidden; 

.... 
} 


- (IBAction)buttonTapped:(UIButton *)sender 
{ 
Model *model = [_array objectAtIndex:sender.tag]; 
model.hidden = YES; 
[table reloadData]; 
} 

就是這樣。

+0

它確實有效!非常感謝。我創建了一個NSMutableDictionary來爲鍵保存一系列buttonModelArray,所以我可以使用indexPath.section和indexPath.row替換代碼中的「標記」。這是一種有效的方法嗎?這是我第一次在stackoverflow上獲得幫助。^^ – studyro 2012-02-29 06:07:33

0

是的,這是因爲細胞再利用。您需要能夠跟蹤tableview中的每個按鈕,可能需要將其與數據源對齊。這可以通過讓您的單元格數據源跟蹤每個按鈕的狀態輕鬆完成。

+0

謝謝你的建議。它的確有幫助! – studyro 2012-02-29 06:08:06