标签和图像视图奇怪地表现在iOS7中的自定义单元格中

时间:2015-05-26 14:51:09

标签: ios objective-c uitableview

我有一个自定义单元格,其中包含标签和图像视图。标签和图像视图根据条件显示。滚动uitableview时,我的自定义单元格奇怪。有一个实例,当我滚动一个标签和imageview时,标签和图像视图会显示,但是当我滚动回到图像视图并标记一个标签并且图像视图将消失时,有时它会重叠另一个图像视图和标签。这是我到目前为止所做的:

cabox@box-codeanywhere:~/workspace/PEP$ python ./dev_scrapers/jordan.py                                                                                                                                                                       
Traceback (most recent call last):                                                                                                                                                                                                            
  File "./dev_scrapers/jordan.py", line 3, in <module>                                                                                                                                                                                        
    from utils import create_entity, create_id, custom_opener                                                                                                                                                                                 
ImportError: No module named utils

我仍然得到相同的结果。我读了另一个解决方案,并在customcell.m文件中移动了我的计算。这是在 layoutSubviews 方法中。

static NSString *simpleTableIdentifier = @"JobDetailCell";
MTJobDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier forIndexPath:indexPath];

if (cell == nil) {
    cell = [[MTJobDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
}

这是我的cellForRowAtIndexPath代码:

NSInteger tempCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"BenefitsCounter"];
NSDictionary *tempDictionary = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"IncentivesAndBenefits"]];
//TEST
NSLog(@"TEMP INT:%ld", (long)tempCount);
NSLog(@"TEMP ARRAY:%@", tempDictionary);

BOOL iFood = tempDictionary[@"food"][@"1"];
NSString* iFoodDescription = tempDictionary[@"food"][@"1"];
BOOL iCommission = tempDictionary[@"commission"][@"1"];
NSString* iCommissionDescription = tempDictionary[@"commission"][@"1"];
BOOL iUniform = tempDictionary[@"uniform"][@"1"];
NSString* iUniformDescription = tempDictionary[@"uniform"][@"1"];
BOOL iTransport = tempDictionary[@"transport"][@"1"];
NSString* iTransportDescription = tempDictionary[@"transport"][@"1"];
BOOL iExtras = tempDictionary[@"extras"][@"1"];
NSString* iExtrasDescription = tempDictionary[@"extras"][@"1"];

//MARK: POSITION labels and imageviews
int img_x = kImgStart_x;
int img_w = kImgStart_w;
int img_h = kImgStart_h;

//result value positions
int lbl_x = kLblStart_x;
int lbl_y = kLblStart_y;
int lbl_w = kLblStart_w;

if(tempCount == 1)
{
    if(iCommission)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iCommissionDescription];

        self.imgCommissionIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgCommissionIncentive.image = [UIImage imageNamed:@"icon-commission.png"];
        self.lblCommissionIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);
    }

    if(iExtras)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iExtrasDescription];

        self.imgExtrasIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgExtrasIncentive.image = [UIImage imageNamed:@"icon-extras.png"];
        self.lblExtrasIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);
    }

    if(iFood)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iFoodDescription];

        self.imgFoodIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgFoodIncentive.image = [UIImage imageNamed:@"icon-food.png"];
        self.lblFoodIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);
    }

    if(iTransport)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iTransportDescription];

        self.imgTransportIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgTransportIncentive.image = [UIImage imageNamed:@"icon-transport.png"];
        self.lblTransportIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);
    }

    if(iUniform)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iUniformDescription];

        self.imgUniformIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgUniformIncentive.image = [UIImage imageNamed:@"icon-uniform2.png"];
        self.lblUniformIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);
    }
}
else if (tempCount > 1)
{
    if(iCommission)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iCommissionDescription];

        self.imgCommissionIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgCommissionIncentive.image = [UIImage imageNamed:@"icon-commission.png"];
        self.imgCommissionIncentive.contentMode = UIViewContentModeScaleAspectFit;
        self.lblCommissionIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);

        lbl_y += kResult_Y_incr;
    }

    if(iExtras)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iExtrasDescription];

        self.imgExtrasIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgExtrasIncentive.image = [UIImage imageNamed:@"icon-extras.png"];
        self.imgExtrasIncentive.contentMode = UIViewContentModeScaleAspectFit;
        self.lblExtrasIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);

        lbl_y += kResult_Y_incr;
    }

    if(iFood)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iFoodDescription];

        self.imgFoodIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgFoodIncentive.image = [UIImage imageNamed:@"icon-food.png"];
        self.imgFoodIncentive.contentMode = UIViewContentModeScaleAspectFit;
        self.lblFoodIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);

        lbl_y += kResult_Y_incr;
    }

    if(iTransport)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iTransportDescription];

        self.imgTransportIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgTransportIncentive.image = [UIImage imageNamed:@"icon-transport.png"];
        self.imgTransportIncentive.contentMode = UIViewContentModeScaleAspectFit;
        self.lblTransportIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);

        lbl_y += kResult_Y_incr;
    }

    if(iUniform)
    {
        //decrement
        tempCount--;
        CGSize expectedSize =[self GetTextHightForLable:iUniformDescription];

        self.imgUniformIncentive.frame = CGRectMake(img_x, lbl_y, img_w, img_h);
        self.imgUniformIncentive.image = [UIImage imageNamed:@"icon-uniform2.png"];
        self.imgUniformIncentive.contentMode = UIViewContentModeScaleAspectFit;
        self.lblUniformIncentive.frame = CGRectMake(lbl_x, lbl_y, lbl_w, expectedSize.height+10);

        lbl_y += kResult_Y_incr;
    }
}

我的代码在iOS 8中完美运行。我真的很困惑为什么它在iOS 7中不起作用。自上周以来,我一直在努力解决这个问题。 有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

你可以尝试下面的代码 -

之后写下面的代码
if (cell == nil) {
    cell = [[MTJobDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
}

for(UIView *view in cell.contentView.subviews){  
        if ([view isKindOfClass:[UIView class]]) {  
            [view removeFromSuperview];   
        }
    }

cell.clipsToBounds = YES;

添加所有UIlabel和UIImage,如下所示

[cell.contentView addSubview:myLabel] ;