滚动期间表视图内容丢失

时间:2015-07-20 05:28:13

标签: ios objective-c scroll tableview reuseidentifier

我正在使用表格视图单元格并在单击单元格中的按钮时添加弹出视图。但是当我滚动表格视图时,弹出窗口正从我的单元格中消失。这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellF
orRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *CellIdentifier =[NSString stringWithFormat:@"ArticleCell%d  %d",indexPath.section,indexPath.row];

    __block ArticleCell_iPad *cell = (ArticleCell_iPad *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[ArticleCell_iPad alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

        [[NSBundle mainBundle] loadNibNamed:@"SynopsysViewSharing" owner:self options:nil];

        cell = [[[ArticleCell_iPad alloc] initWithFrame:CGRectMake(0, 0, kCellWidth_iPad, kCellHeight_iPad)] autorelease];

        __block NSDictionary *currentArticle = [self.articles objectAtIndex:indexPath.section];

         [cell.iconImage loadImageWithURL:[[currentArticle objectForKey:@"sourceLogoImg"]objectForKey:@"text"]];


        [cell.sideButton setBackgroundImage:[UIImage imageNamed:[cellButtonImageArr objectAtIndex:self.selectIndex]]

        [cell.sideButton addTarget:self action:@selector(sideButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

        cell.sideButton.tag = indexPath.section;

        TableViewCellSelectionStyleNone;

   }
   return cell;
}

按钮操作:

 -(IBAction)sideButtonClicked:(UIButton*)sender
 {

    buttonShare=[[[NSBundle mainBundle] loadNibNamed:@"SynopsysViewSharing" owner:self options:nil] lastObject];

    NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:sender.tag];
    ArticleCell_iPad *cellSelected = (ArticleCell_iPad*)[_horizontalTableView cellForRowAtIndexPath:myIP];

    [cellSelected.contentView addSubview:buttonShare];
    buttonShare.alpha = 0.0f;
    buttonShare.tag=500;
    [buttonShare setFrame:CGRectMake(0,600, cellSelected.frame.size.height,55)];

    [UIView animateWithDuration:0.5 animations:^{
    [buttonShare setFrame:CGRectMake(0,cellSelected.frame.size.width-55, cellSelected.frame.size.height,55)];
    buttonShare.alpha = 1.0f;
   } completion:^(BOOL finished) {

   }];
}

2 个答案:

答案 0 :(得分:0)

而是通过编码将 buttonhare 添加到内容视图中,请尝试添加“按钮共享”#39;在原型单元本身的故事板中,通过检查隐藏属性使其隐藏。

点击UITableviewCell中的按钮,将 buttonshare的隐藏属性设置为" NO"在sideButtonClicked:

希望有所帮助:)

答案 1 :(得分:0)

您的实施中存在两个主要问题:

  1. 在您的cellForRowAtIndexPath中,您需要初始化给定indexPath的单元格,即使它是重用的单元格(如果cell != nil)。
  2. 在您的sideButtonClicked中,执行调用cellForRowAtIndexPath以查找该单元格。它将创建一个全新的单元格,而不是返回屏幕上的单元格。而是从方法的UIButton参数中获取它是哪一行。
  3. 看看Apple's programming guide