3

ここにサブビューを追加することに関するAppleのドキュメントに従いましたUITableViewcell

セルに 3 番目のラベルを追加します。問題は、標準のチェックマーク アクセサリ ビューを右側に表示すると、3 番目のラベルを表示していなくても、セルの内容全体が左に移動することです。以下のスクリーンショット写真

そして、ここに私のコードがあります:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"MyCell";

    UILabel *mainLabel, *secondLabel, *thirdLabel;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    cell.accessoryType = UITableViewCellAccessoryNone;

    mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 2.0, 100.0, 21.0)];
    mainLabel.tag = MAINLABEL_TAG;
    //mainLabel.font = [UIFont systemFontOfSize:14.0];
    //mainLabel.font = [UIFont fontWithName:@"Geeza Pro Bold" size:17.0];
    mainLabel.font = [UIFont boldSystemFontOfSize:17];
    mainLabel.textAlignment = UITextAlignmentLeft;
    mainLabel.textColor = [UIColor blackColor];
    mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:mainLabel];

    secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 22.0, 100.0, 21.0)];
    secondLabel.tag = SECONDLABEL_TAG;
    //secondLabel.font = [UIFont systemFontOfSize:12.0];
    //secondLabel.font = [UIFont fontWithName:@"Geeza Pro" size:15.0];
    secondLabel.font = [UIFont systemFontOfSize:15];
    secondLabel.textAlignment = UITextAlignmentLeft;
    secondLabel.textColor = [UIColor darkGrayColor];
    secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:secondLabel];

    thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(203.0, 11.0, 70.0, 21.0)];
    thirdLabel.tag = THIRDLABEL_TAG;
    //thirdLabel.font = [UIFont systemFontOfSize:12.0];
    //thirdLabel.font = [UIFont fontWithName:@"Geeza Pro Bold" size:17.0];
    thirdLabel.font = [UIFont boldSystemFontOfSize:17];
    thirdLabel.textAlignment = UITextAlignmentRight;
    thirdLabel.textColor = [UIColor darkGrayColor];
    thirdLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
    [cell.contentView addSubview:thirdLabel];


}
    else
    {
        mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
        secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
        thirdLabel = (UILabel *)[cell.contentView viewWithTag:THIRDLABEL_TAG];
    }

    Car *car = [self.dataModel carAtIndex:indexPath.row];
   NSString *carName = [NSString stringWithFormat:@"%@",car.deviceName];
   NSString *carDetails = [NSString stringWithFormat:@"%@ at %@",car.date,car.location];
   NSString *carSpeed = [NSString stringWithFormat:@"%@ km/h",car.speed];


   mainLabel.text = carName;
   secondLabel.text = carDetails;
   thirdLabel.text = carSpeed;

return cell;
}

更新: @MattG のアドバイスに従ってに変更UIViewAutoresizingFlexibleLeftMarginしました。UIViewAutoresizingFlexibleWidthセルを選択すると、ラベルは左に移動せず、下の図のように部分的に覆われます。ここに画像の説明を入力

多分これは私の原因- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathですか?そのコードは次のとおりです。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    *)indexPath
{

    if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
    }
    else
    {
        [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
    }
}
4

2 に答える 2