2015-08-21 73 views
0

問: 當我在iOS8上創建UITableView +它的作品,因爲我想,但是當我在iOS7用相同的代碼創建UITableViewUITableViewCell產生了一些問題。的UITableViewCell在iOS7創建問題


這是我的CODE

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

+0

爲什麼DEFAULT_CELL_HEIGHT/4); –

+0

@mihirmehta,DEFAULT_CELL_HEIGHT = 200和DEFAULT_CELL_HEIGHT/4是在用戶選擇任何行之前將被隱藏的視圖。 – Vats

+0

你可以檢查CGRect的值rect = cell.contentView.frame;在ios7中? –

回答

0

檢查,如果你返回正確的高度

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