如何强制uitableview重新布局单元格子视图

时间:2012-03-11 11:12:39

标签: uitableview

我有一个我自己布局单元格的uitableviw,我想强制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;

} 

当我们使用nib文件创建这个自定义单元格并为每个标签设置autoresizingMask时,当界面方向改变时它会更新位置,但是当我们在代码中创建自定义单元格时,autoresizingMask无法正常工作。

1 个答案:

答案 0 :(得分:0)

你还需要UIViewAutoresizingFlexibleWidth才能正常工作(他说,不能先测试......)