使用标签处理时未正确设置UIButton图像

时间:2013-07-10 15:50:11

标签: ios uitableview tags uibutton uiimage

在原型单元格中,由于没有IBOutlet可以给出,所以我在带有标签的UI按钮上得到了一个句柄。但是,在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是私有的setter,因此无法动态更改。如何使按钮图像与条件一致?

3 个答案:

答案 0 :(得分: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];
    }

答案 1 :(得分:1)

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

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

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

但这种改变不应该解决你的问题。如果要在cellForRowIndexPath中发布所有代码,将会很有帮助。我怀疑问题是在你调用dequeueReusableCellWithIdentifier的地方,然后如果nil创建单元格。

答案 2 :(得分:0)

我得到了解决方案: 实际上在IBAction方法中,我将popover绑定到我的infoButton。 - (IBAction)infoButtonAction:(id)发送者事件:(id)事件{

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;

}

每当我按下表格中的任何按钮时,信息按钮标签就会改变。所以我添加了最后一行,现在很好。无论如何,Thanx ..