0

我已经自定义UITableViewCell了在代码中动态放置按钮的地方。我已经以编程方式为这些按钮设置了动作,当动作被触发时,我需要重新加载表格等等。一旦调用重新加载,发件人按钮就会消失,我不知道为什么......有人可以给我提示可能的原因吗?如果没有点击按钮,并且我在表格中上下滚动,它仍然存在。

谢谢!

编辑

这是来自的代码片段cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSArray *listData =[self.tableViewEntries objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];

   UITableViewCell *cell;

   if (indexPath.section == 0) {

    switch (indexPath.row) {
        case 0: {
            cell = [self.tableView dequeueReusableCellWithIdentifier:@"firstCell"];
            ((CustomCell *)cell).cellTextField.tag = firstCellTag;
            cell.tag = firstCellTag;
            break;
        }
        case 1: {
            cell = [self.tableView dequeueReusableCellWithIdentifier:@"secondCell"];
            ((CustomCell *)cell).cellTextField.tag = secondCellTag;
            cell.tag = secondCellTag;
            break;
        }
   } 
   return cell;
}

CustomCell我定义的从nib. 这些单元格有一个UITextField用户输入和一个UIView我根据文本字段中的用户输入在代码中动态设置或删除按钮的位置。按钮是在代码中创建的,它们的动作也是在代码中设置的:

- (void)setButtonForCell:(UITableViewCell *)cell withResult:(BOOL)isValid
{
   UIImage* validationButtonNormalImage = nil;
   UIImage* validationButtonHighlightImage = nil;

   UIButton *validationButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [validationButton setFrame:CGRectMake(0, 0, ((CustomCell *)cell).cellValidationView.frame.size.width, ((CustomCell *)cell).cellValidationView.frame.size.height)];

   if (isValid) {
       validationButtonNormalImage =[[UIImage imageNamed:@"success.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
       validationButtonHighlightImage =[[UIImage imageNamed:@"success.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];

      [validationButton removeTarget:self
                            action:@selector(validationButtonTapped:)
                  forControlEvents:UIControlEventTouchUpInside];
   }
   else {
       validationButtonNormalImage =[[UIImage imageNamed:@"error.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
       validationButtonHighlightImage =[[UIImage imageNamed:@"error.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];

      [validationButton setTag:cell.tag];

      [validationButton removeTarget:self
                            action:@selector(validationButtonTapped:)
                  forControlEvents:UIControlEventTouchUpInside];

      [validationButton addTarget:self
                         action:@selector(validationButtonTapped:)
               forControlEvents:UIControlEventTouchUpInside];
   }


   [validationButton setBackgroundImage:validationButtonNormalImage forState:UIControlStateNormal];
   [validationButton setBackgroundImage:validationButtonHighlightImage forState:UIControlStateHighlighted];


   [((CustomCell *)cell).cellValidationView.subviews makeObjectsPerformSelector: @selector(removeFromSuperview)];
   [((CustomCell *)cell).cellValidationView addSubview:validationButton];
}

然后,在 中validationButtonTapped:,我执行了一些任务并调用[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:NO];,当我从视图中松开按钮时。但是,如果我不点击该按钮并再次向下和向上滚动以再次调用“cellForRowAtIndexPath:”,则该按钮仍保留在此处。就像我只是在要求重新加载时才松开它。

有什么帮助吗?

4

1 回答 1

0

cellForRowAtIndexPath:如果您遵循通常的模式,通常会重复使用单元格。您是否删除了每个单元格的 prepareForReuse 中的所有子视图?如果你不这样做,那么当你上下滚动时,你会在单元格上看到意想不到的按钮。cellForRowAtIndexPath:. _ _

于 2013-08-06T06:40:39.093 回答