0

在原型单元格中,因为没有 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 是一个私有设置器,因此不能动态更改。如何使按钮图像与条件一致?

4

3 回答 3

1

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

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

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

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

于 2013-07-10T21:22:56.977 回答
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];
    }
于 2013-07-10T16:32:49.437 回答
0

我得到了解决方案:实际上在 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;

}

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

于 2013-07-10T21:52:27.380 回答