iOS7中的UITableViewCell创建问题

时间:2015-08-21 09:03:53

标签: ios objective-c uitableview ios8.3

问题: 当我在iOS8 +中创建UITableView时,它可以正常工作,但是当我在iOS7中使用相同的代码创建UITableView时,会在UITableViewCell上创建一些问题。

这是我的代码

 NSString* identifier = [NSString stringWithFormat:@"%ld",(long)tableView.tag];
        UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        if(!cell){
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.backgroundColor = [UIColor clearColor];
        for(UIView* subview in cell.contentView.subviews){
            if(subview.tag == -1){
                [subview removeFromSuperview];
            }
        }
        NSDictionary* info = [array objectAtIndex:indexPath.row];
        static float span = 5;

        //Cell background
        CGRect rect = cell.contentView.frame;
        CGRect subviewRect =CGRectMake(rect.origin.x+span,rect.origin.y+span,rect.size.width-(span*2),(DEFAULT_CELL_HEIGHT/2)-(span*2));
        CGRect dummysubviewRect = CGRectMake(rect.origin.x+span,rect.origin.y+span,rect.size.width-(span*2),rect.size.height-(span*2));
        UIView* subview = [[UIView alloc]initWithFrame:dummysubviewRect];
        subview.tag = -1;
        subview.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:subview];

        //DateView background
        CGRect dateViewRect = CGRectMake(span,span,subviewRect.size.height-(span*2),subviewRect.size.height-(span*2));
        UIView*dateView = [[UIView alloc]initWithFrame:dateViewRect];
        dateView.backgroundColor = COLOR;
        [subview addSubview:dateView];
        float DAY_NUMBER_HEIGHT = dateViewRect.size.height/3;
        float DAY_NAME_HEIGHT = dateViewRect.size.height/4;

        //DAY_NAME
        CGRect daynameRect = CGRectMake(span,span,dateViewRect.size.width,DAY_NAME_HEIGHT);
        UILabel* day_name = [[UILabel alloc]initWithFrame:daynameRect];
        day_name.textAlignment = NSTextAlignmentCenter;
        day_name.font = [UIFont fontWithName:FONT size:14];
        day_name.textColor = [UIColor whiteColor];
        NSString* name = [info valueForKey:@"copied_text_day"];
        day_name.text = name;
        [subview addSubview:day_name];

        //DAY_NUMBER
        float yPosition = span+(dateViewRect.size.height/2)-(DAY_NUMBER_HEIGHT/2);
        CGRect daynumberRect = CGRectMake(span,yPosition,dateViewRect.size.width,DAY_NUMBER_HEIGHT);
        UILabel* day_number = [[UILabel alloc]initWithFrame:daynumberRect];
        day_number.textAlignment = NSTextAlignmentCenter;
        day_number.font = [UIFont fontWithName:@"HelveticaNeue" size:DAY_NUMBER_HEIGHT];
        day_number.textColor = [UIColor whiteColor];
        NSString* number = [info valueForKey:@"copied_text_date"];
        number = [self getDayNumberFromDate:number];
        day_number.text = number;
        [subview addSubview:day_number];

        //DAY_TIME
        CGRect daytimeRect = CGRectMake(span,(dateViewRect.size.height-DAY_NAME_HEIGHT)+span,dateViewRect.size.width,DAY_NAME_HEIGHT);
        UILabel* day_time = [[UILabel alloc]initWithFrame:daytimeRect];
        day_time.textAlignment = NSTextAlignmentCenter;
        day_time.font = [UIFont fontWithName:FONT size:14];
        day_time.textColor = [UIColor whiteColor];
        NSString* time = [info valueForKey:@"copied_text_time"];
        time = [self getTimeFromDate:time];
        day_time.text = time;
        [subview addSubview:day_time];

        //text textView
        float xPosition = span+dateViewRect.size.width+span;
        CGRect textRect = CGRectMake(xPosition,span,subviewRect.size.width-xPosition-span,subviewRect.size.height-(span*2));
        UITextView* textview = [[UITextView alloc]initWithFrame:textRect];
        textview.backgroundColor = [UIColor clearColor];
        textview.font = [UIFont fontWithName:FONT size:15];
        textview.textColor = COLOR;
        textview.userInteractionEnabled = false;
        [subview addSubview:textview];
        NSString*text = [info valueForKey:@"copied_text"];
        textview.text = text;

        //line
        CGRect lineRect = CGRectMake(0,subviewRect.size.height,subviewRect.size.width,2);
        UIView* line = [[UIView alloc]initWithFrame:lineRect];
        line.backgroundColor = COLOR;
        [subview addSubview:line];

        CGRect viewRect = CGRectMake(0,(DEFAULT_CELL_HEIGHT/2)-8,subviewRect.size.width, DEFAULT_CELL_HEIGHT/4);
        UIView *myView =[self createViewWithRect:viewRect WithTag:(int)indexPath.row];
        myView.backgroundColor = [UIColor whiteColor];
        [subview addSubview:myView];
        if([selectedRow isEqual:indexPath]){
            [UIView animateWithDuration:0.2 animations:^{
                myView.alpha=1;
            }];

        }else{
            [UIView animateWithDuration:0.2 animations:^{
                myView.alpha=0;
            }];

        }
        return cell;


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if([selectedRow isEqual:indexPath]){
        float h =DEFAULT_CELL_HEIGHT/2+DEFAULT_CELL_HEIGHT/4;
        return h;
    }else{
        float h =DEFAULT_CELL_HEIGHT/2;
        return h;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if([selectedRow isEqual:indexPath]){
        selectedRow = nil;
        [UIView animateWithDuration:0.2 animations:^{
            [[cell viewWithTag:indexPath.row]setAlpha:1];
        }];

    }else{
        selectedRow = indexPath;
        [UIView animateWithDuration:0.2 animations:^{
            [[cell viewWithTag:indexPath.row]setAlpha:0];
        }];

    }
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
}

这里是屏幕拍摄

  在iOS8 +中

  
enter image description here   
  在iOS7中   
enter image description here

1 个答案:

答案 0 :(得分:0)

检查您是否返回正确的身高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath(NSIndexPath *)indexPath {
    return 65.0f;
}