2013-07-10 75 views
0

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

在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是一个私人设置器,所以不能动态改变。我如何使按钮图像与条件一致?

+0

为什么不直接在xib或storyboard中设置按钮的图像。你说你有2个原型单元。 – Adithya

+0

我认为你在做tableviews错误.. –

+0

2原型单元格有不同的列,并将在具有不同特权的不同用户下使用。该按钮仅在有数据时才会启用。无论如何,我已经发布了下面的修复。感谢名单。 – ShaluRaj

回答

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]; 
    } 
+0

我试过使用相同的标签也..但仍然是行为是随机的... – ShaluRaj

1

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

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

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

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

0

我得到了这个soln: 实际上在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反正..