重新加载表时,在自定义单元格内丢失UIButton

时间:2013-08-06 06:37:05

标签: ios uitableview uibutton reload

我已经自定义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加载的。这些单元格的用户输入为UITextFieldUIView根据文本字段中的用户输入在代码中动态设置或删除按钮。按钮是在代码中创建的,它们的动作也在代码中设置:

- (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:'按钮保持在此处。就像我只是在要求重新加载时才松开它。

任何帮助?

1 个答案:

答案 0 :(得分:0)

如果您遵循通常的cellForRowAtIndexPath:模式,则通常会重复使用单元格。您是否删除了每个单元格的prepareForReuse中的所有子视图?如果没有,那么当您向上和向下滚动时,您会在单元格上看到意外的按钮。您可能需要在之后在每个单元格的配置中添加按钮,以便在cellForRowAtIndexPath:中获取要重复使用的单元格(或更新的代码,使单元格出列)。