2012-03-11 206 views
0

我有一個uitableviw我自己佈局的細胞,我想強制的UITableView當設備旋轉到另一個界面取向重新佈局的細胞子視圖....如何強制的UITableView到重新佈局細胞子視圖

的細胞是像下面

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UILabel *itemCodeLabel, *itemNameLabel, *quantityTextLabel, *quantityValueLabel, *measurementTextLabel, *measurementValueLabel, *fieTextLabel , *fieValueLabel, *amountTextLabel, *amountValueLabel; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
     cell.selectionStyle = UITableViewCellSelectionStyleGray; 

     itemCodeLabel = [[[UILabel alloc] initWithFrame:CGRectMake(250.0, 5.0, 60.0, 20.0)] autorelease]; 
     itemCodeLabel.tag = itemCodeLabel_TAG; 
     itemCodeLabel.font = [UIFont fontWithName:@"Courier New" size:9.0]; 
     itemCodeLabel.textAlignment = UITextAlignmentRight; 
     itemCodeLabel.textColor = [UIColor blueColor]; 
     itemCodeLabel.lineBreakMode = UILineBreakModeTailTruncation; 
     itemCodeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     [cell.contentView addSubview:itemCodeLabel]; 

     itemNameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, 245.0, 20.0)] autorelease]; 
     itemNameLabel.tag = itemNameLabel_TAG; 
     itemNameLabel.font = [UIFont fontWithName:@"Courier New" size:12.0]; 
     itemNameLabel.textAlignment = UITextAlignmentRight; 
     itemNameLabel.textColor = [UIColor darkTextColor]; 
     itemNameLabel.lineBreakMode = UILineBreakModeTailTruncation; 
     itemNameLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     [cell.contentView addSubview:itemNameLabel]; 

     float width = self.view.bounds.size.width; 

     NSLog(@"alignment width : %f" , width); 

     quantityTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(width - 70, 25.0, 65.0, 20.0)] autorelease]; 
     quantityTextLabel.tag = quantityTextLabel_TAG; 
     quantityTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0]; 
     quantityTextLabel.textAlignment = UITextAlignmentCenter; 
     quantityTextLabel.textColor = [UIColor darkTextColor]; 
     quantityTextLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  quantityTextLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     quantityTextLabel.text = @"تعداد ارسال"; 
     [cell.contentView addSubview:quantityTextLabel]; 

     quantityValueLabel = [[[UILabel alloc] initWithFrame:CGRectMake(width - 70, 50.0, 65.0, 20.0)] autorelease]; 
     quantityValueLabel.tag = quantityValueLabel_TAG; 
     quantityValueLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
     quantityValueLabel.textAlignment = UITextAlignmentCenter; 
     quantityValueLabel.textColor = [UIColor darkTextColor]; 
     quantityValueLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  quantityValueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     [cell.contentView addSubview:quantityValueLabel]; 

     measurementTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(width/2 + 30, 25.0, 50.0, 20.0)] autorelease]; 
     measurementTextLabel.tag = measurementTextLabel_TAG; 
     measurementTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0]; 
     measurementTextLabel.textAlignment = UITextAlignmentCenter; 
     measurementTextLabel.textColor = [UIColor darkTextColor]; 
     measurementTextLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  measurementTextLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     measurementTextLabel.text = @"واحد"; 
     [cell.contentView addSubview:measurementTextLabel]; 

     measurementValueLabel = [[[UILabel alloc] initWithFrame:CGRectMake(width/2 + 30, 50.0, 50.0, 20.0)] autorelease]; 
     measurementValueLabel.tag = measurementValueLabel_TAG; 
     measurementValueLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
     measurementValueLabel.textAlignment = UITextAlignmentCenter; 
     measurementValueLabel.textColor = [UIColor darkTextColor]; 
     measurementValueLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  measurementValueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     measurementValueLabel.text = @"عدد"; 
     [cell.contentView addSubview:measurementValueLabel]; 

     fieTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(width/2 - 80, 25.0, 50.0, 20.0)] autorelease]; 
     fieTextLabel.tag = fieTextLabel_TAG; 
     fieTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0]; 
     fieTextLabel.textAlignment = UITextAlignmentCenter; 
     fieTextLabel.textColor = [UIColor darkTextColor]; 
     fieTextLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  fieTextLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     fieTextLabel.text = @"فی-ریال"; 
     [cell.contentView addSubview:fieTextLabel]; 

     fieValueLabel = [[[UILabel alloc] initWithFrame:CGRectMake(width/2 - 80, 50.0, 50.0, 20.0)] autorelease]; 
     fieValueLabel.tag = fieValueLabel_TAG; 
     fieValueLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
     fieValueLabel.textAlignment = UITextAlignmentCenter; 
     fieValueLabel.textColor = [UIColor darkTextColor]; 
     fieValueLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  fieValueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     [cell.contentView addSubview:fieValueLabel]; 

     amountTextLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 25.0, 70.0, 20.0)] autorelease]; 
     amountTextLabel.tag = amountTextLabel_TAG; 
     amountTextLabel.font = [UIFont fontWithName:@"Arial" size:12.0]; 
     amountTextLabel.textAlignment = UITextAlignmentCenter; 
     amountTextLabel.textColor = [UIColor darkTextColor]; 
     amountTextLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  amountTextLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     amountTextLabel.text = @"مبلغ-ریال"; 
     [cell.contentView addSubview:amountTextLabel]; 

     amountValueLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10.0, 50.0, 70.0, 20.0)] autorelease]; 
     amountValueLabel.tag = amountValueLabel_TAG; 
     amountValueLabel.font = [UIFont boldSystemFontOfSize:12.0]; 
     amountValueLabel.textAlignment = UITextAlignmentCenter; 
     amountValueLabel.textColor = [UIColor darkTextColor]; 
     amountValueLabel.lineBreakMode = UILineBreakModeTailTruncation; 
//  amountValueLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     [cell.contentView addSubview:amountValueLabel]; 



    } else { 

     itemCodeLabel = (UILabel *)[cell.contentView viewWithTag:itemCodeLabel_TAG]; 
     itemNameLabel = (UILabel *)[cell.contentView viewWithTag:itemNameLabel_TAG]; 
     quantityTextLabel = (UILabel *)[cell.contentView viewWithTag:quantityTextLabel_TAG]; 
     quantityValueLabel = (UILabel *)[cell.contentView viewWithTag:quantityValueLabel_TAG]; 
     measurementTextLabel = (UILabel *)[cell.contentView viewWithTag:measurementTextLabel_TAG]; 
     measurementValueLabel = (UILabel *)[cell.contentView viewWithTag:measurementValueLabel_TAG]; 
     fieTextLabel = (UILabel *)[cell.contentView viewWithTag:fieTextLabel_TAG]; 
     fieValueLabel = (UILabel *)[cell.contentView viewWithTag:fieValueLabel_TAG]; 
     amountTextLabel = (UILabel *)[cell.contentView viewWithTag:amountTextLabel_TAG]; 
     amountValueLabel = (UILabel *)[cell.contentView viewWithTag:amountValueLabel_TAG]; 
    } 

    quantityTextLabel.text = @"تعداد ارسال"; 
    measurementTextLabel.text = @"واحد"; 
    measurementValueLabel.text = @"عدد"; 
    fieTextLabel.text = @"فی-ریال"; 
    amountTextLabel.text = @"مبلغ-ریال"; 

// if(indexPath.row > 3) 
//  needsRefresh = false; 

    itemCodeLabel.text = [NSString stringWithFormat:@"[%@]" , [[self.dataSourceArray objectAtIndex:indexPath.row] valueForKey:@"ItemCode"]]; 

    itemNameLabel.text = [NSString stringWithFormat:@"[%@]" , [[self.dataSourceArray objectAtIndex:indexPath.row] valueForKey:@"ItemName"]]; 

    quantityValueLabel.text = [NSString stringWithFormat:@"%i" , [[[self.dataSourceArray objectAtIndex:indexPath.row] valueForKey:@"SumQuantity"] intValue]]; 

    fieValueLabel.text = [formatter stringFromNumber:[[self.dataSourceArray objectAtIndex:indexPath.row] valueForKey:@"Fee"]]; 

    amountValueLabel.text = [formatter stringFromNumber:[[self.dataSourceArray objectAtIndex:indexPath.row] valueForKey:@"Amount"]]; 

    return cell; 

} 

當我們創建與筆尖文件這個自定義單元格,並設置autoresizingMask每個標籤,當界面取向的變化更新它的位置,但是當我們在代碼中創建一個自定義單元格,因爲我做的autoresizingMask無法正常工作。

回答

0

您還需要UIViewAutoresizingFlexibleWidth使其正常工作(他說,不能先測試....)