【问题标题】:UIButton image not properly set when handled with tags使用标签处理时未正确设置 UIButton 图像
【发布时间】:2013-07-10 15:50:10
【问题描述】:

在原型单元格中,因为没有 IBOutlet 可以给出,所以我在我的 UIbutton 上获得了一个带有标签的句柄。但是在cellForRowAtIndexPath 方法中设置按钮的图像时,它没有显示正确的启用和禁用状态图像。

在 cellForRowIndexPath 中:

if([cellIdentifier isEqualToString:CELL_ID_COMPLIANCE])
    _infoButton = (UIButton *)[cell viewWithTag:800];
else
    _infoButton = (UIButton *)[cell viewWithTag:900];

if([[comments objectAtIndex:kEntitlementComment] length] > 0){
    _infoButton.enabled= YES;
    [_infoButton setImage:[UIImage imageNamed:@"icon_info.png"] forState:UIControlStateNormal];
    TRACELOG(@"the coments :%@", [comments objectAtIndex:kEntitlementComment]);
    }
else{
    _infoButton.enabled= NO;
    [_infoButton setImage:[UIImage imageNamed:@"info_icon_disabled.png"] forState:UIControlStateDisabled];
    }

我在表格中有两种原型单元格,但按钮图像只有两种。我也试过了

[self performSelectorOnMainThread:@selector(doButtonSettings:) 

Object:textArray waitUntilDone:YES]; 

在选择器中,我正在设置按钮的图像。如您所知,buttonType 是一个私有设置器,因此不能动态更改。如何使按钮图像符合条件?

【问题讨论】:

  • 为什么不直接在xib或者storyboard中设置按钮的图片。你说你有 2 个原型单元。
  • 我认为你做的 tableviews 错了..
  • 这两个原型单元格有不同的列,将在具有不同权限的不同用户下使用。该按钮仅在有数据时才会启用。无论如何,我已经在下面发布了修复程序。谢谢。

标签: ios uitableview tags uibutton uiimage


【解决方案1】:

为什么不对按钮使用相同的标签?

//All prototype cells info buttons have the same tag
_infoButton = (UIButton *)[cell viewWithTag:900];

if([[comments objectAtIndex:kEntitlementComment] length] > 0){
    _infoButton.enabled= YES;
    [_infoButton setImage:[UIImage imageNamed:@"icon_info.png"] forState:UIControlStateNormal];
    TRACELOG(@"the coments :%@", [comments objectAtIndex:kEntitlementComment]);
    }
else{
    _infoButton.enabled= NO;
    [_infoButton setImage:[UIImage imageNamed:@"info_icon_disabled.png"] forState:UIControlStateDisabled];
    }

【讨论】:

    【解决方案2】:

    将对 setImage 的调用移动到创建单元格的位置。为所有按钮设置正常和禁用状态的图像。

    在你的 if 语句中你只需要切换按钮状态

    if([[comments objectAtIndex:kEntitlementComment] length] > 0){
      _infoButton.enabled= YES;
    }
    else{
      _infoButton.enabled= NO;
    }
    

    但仅此更改不能解决您的问题。如果您将所有代码发布在 cellForRowIndexPath 中,将会很有帮助。我怀疑问题出在您调用 dequeueReusableCellWithIdentifier 的地方,然后如果 nil 创建单元格。

    【讨论】:

      【解决方案3】:

      我得到了答案: 实际上,在 IBAction 方法中,我将弹出框绑定到我的 infoButton。 - (IBAction)infoButtonAction:(id)sender event:(id) event {

      NSSet *touches = [event allTouches];
      UITouch *touch = [touches anyObject];
      CGPoint touchPoint = [touch locationInView:_summaryTableView];
      NSIndexPath *indexPath = [_summaryTableView indexPathForRowAtPoint:touchPoint];
      UITableViewCell *cell  = [_summaryTableView cellForRowAtIndexPath:indexPath];
      
      _infoButton.tag=indexPath.row;
      
      if ( [self.myPopoverController isPopoverVisible] )
      {
          // Close the popover view if toolbar button was touched again and popover is already visible
          [self.myPopoverController dismissPopoverAnimated: YES];
          return;
      }
      
      UIStoryboard                     *storyboard     = [UIStoryboard storyboardWithName: @"iPad" bundle: nil];
      NSITSLMEntitlementSummCommentsViewController    *popoverContent = [storyboard instantiateViewControllerWithIdentifier: @"slm_ES_Comments"];
      
      self.myPopoverController =   [[UIPopoverController alloc] initWithContentViewController: popoverContent];
      popoverContent.comments.text  = [[self.dataController getSummaryRecordAtIndex:indexPath.row] objectAtIndex:kEntitlementComment];
      
       // Present the popover view non-modally with a refrence to the toolbar button which was pressed
      if([popoverContent.comments.text length] > 0)
          [self.myPopoverController presentPopoverFromRect:[sender frame] inView:cell permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
      _infoButton.tag= 800;
      

      }

      每当我按下表格中的任何按钮时,信息按钮标签都会发生变化。所以我添加了最后一行,现在它很好。无论如何还是谢谢..

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多