2012-02-28 63 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