1

私がやろうとしているのは、静的データを取得して、のデータを表示することですUITableView。以下は私が持っているものです。

注::英語版とアラビア語版のアプリがあるので、プログラムでセルにlabelとimageViewを追加しています。

//
//  ListViewController.m
//  Demo
//
//  Created by  on 1/20/13.
//  Copyright (c) 2013 . All rights reserved.
//

#import "ListViewController.h"
#import "ListOfProperty.h"
#import "Localization.h"
#import <QuartzCore/QuartzCore.h>

@interface ListViewController ()

@end

@implementation ListViewController {
    NSMutableArray *propListFinal;
}

@synthesize headerLabel, secondImage, myTableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void) viewWillAppear:(BOOL)animated {

    NSLog(@"in viewWillAppear");

    secondImage.alpha = 0.4f;

    propListFinal = [[NSMutableArray alloc] initWithCapacity:2];

    ListOfProperty *listOfProperty;

    listOfProperty = [[ListOfProperty alloc] init];
    listOfProperty.refIDOfFlat = @"8756342";
    listOfProperty.addressOfFlat = @"Al Salam, Hawalli, Kuwait";
    listOfProperty.amountOfFlat = @"2500";
    listOfProperty.typeOfFlat = @"Villa";
    listOfProperty.areaOfFlat = @"300";
    listOfProperty.imageName = @"home0.jpg";
    [propListFinal addObject:listOfProperty];

    listOfProperty = [[ListOfProperty alloc] init];
    listOfProperty.refIDOfFlat = @"6342875";
    listOfProperty.addressOfFlat = @"Al Salam, Khaitan, Kuwait";
    listOfProperty.amountOfFlat = @"2300";
    listOfProperty.typeOfFlat = @"Villa";
    listOfProperty.areaOfFlat = @"400";
    listOfProperty.imageName = @"home1.jpg";
    [propListFinal addObject:listOfProperty];

    listOfProperty = [[ListOfProperty alloc] init];
    listOfProperty.refIDOfFlat = @"8573624";
    listOfProperty.addressOfFlat = @"Hateen, Hawalli, Kuwait";
    listOfProperty.amountOfFlat = @"1500";
    listOfProperty.typeOfFlat = @"House";
    listOfProperty.areaOfFlat = @"415";
    listOfProperty.imageName = @"home2.jpg";
    [propListFinal addObject:listOfProperty];

    listOfProperty = [[ListOfProperty alloc] init];
    listOfProperty.refIDOfFlat = @"8634752";
    listOfProperty.addressOfFlat = @"Hateen, Salmiya, Kuwait";
    listOfProperty.amountOfFlat = @"1300";
    listOfProperty.typeOfFlat = @"Flat";
    listOfProperty.areaOfFlat = @"325";
    listOfProperty.imageName = @"home3.jpg";
    [propListFinal addObject:listOfProperty];

    listOfProperty = [[ListOfProperty alloc] init];
    listOfProperty.refIDOfFlat = @"8427563";
    listOfProperty.addressOfFlat = @"Al Salam, Jaleeb, Kuwait";
    listOfProperty.amountOfFlat = @"2500";
    listOfProperty.typeOfFlat = @"Small Flat";
    listOfProperty.areaOfFlat = @"100";
    listOfProperty.imageName = @"home4.jpg";
    [propListFinal addObject:listOfProperty];

    listOfProperty = [[ListOfProperty alloc] init];
    listOfProperty.refIDOfFlat = @"8427561";
    listOfProperty.addressOfFlat = @"Al Salam, Mallya, Kuwait";
    listOfProperty.amountOfFlat = @"2500";
    listOfProperty.typeOfFlat = @"Small Flat";
    listOfProperty.areaOfFlat = @"100";
    listOfProperty.imageName = @"home5.jpg";
    [propListFinal addObject:listOfProperty];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (int)numberOfSectionsInTableView:(UITableView *) tableView {
    return 1;
}

- (int) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger)section {
    return [propListFinal count];
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
    }

    NSString *commString;

    NSString *myLanguage = localize(@"myLanguage");

    ListOfProperty *propList = [propListFinal objectAtIndex:indexPath.row];

    UIImageView *myImageview;
    UILabel *addressLabel;
    UILabel *typeLabel;
    UILabel *areaLabel;
    UILabel *priceLabel;

    if ([myLanguage isEqualToString:@"en"]) {
        myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 75, 65)];
        addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(87, 9, 223, 21)];
        typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(87, 32, 223, 21)];
        areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(87, 55, 223, 21)];
        priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 74, 110, 21)];
    } else {
        myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(240, 004, 75, 65)];
        addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, 9, 223, 21)];
        typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, 32, 223, 21)];
        areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, 55, 223, 21)];
        priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 74, 110, 21)];
        addressLabel.textAlignment = NSTextAlignmentRight;
        typeLabel.textAlignment = NSTextAlignmentRight;
        areaLabel.textAlignment = NSTextAlignmentRight;
        priceLabel.textAlignment = NSTextAlignmentRight;
    }

    [addressLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 13.0]];
    [typeLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 13.0]];
    [areaLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 13.0]];
    [priceLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 15.0]];
    [priceLabel setFont:[UIFont boldSystemFontOfSize:15.0]];


    addressLabel.backgroundColor = [UIColor clearColor];
    typeLabel.backgroundColor = [UIColor clearColor];
    areaLabel.backgroundColor = [UIColor clearColor];
    priceLabel.backgroundColor = [UIColor clearColor];

    addressLabel.textColor = [UIColor colorWithRed:(246/255.0) green:(230/255.0) blue:(74/255.0) alpha:1.0f];
    typeLabel.textColor = [UIColor colorWithRed:(246/255.0) green:(230/255.0) blue:(74/255.0) alpha:1.0f];
    areaLabel.textColor = [UIColor colorWithRed:(246/255.0) green:(230/255.0) blue:(74/255.0) alpha:1.0f];
    priceLabel.textColor = [UIColor colorWithRed:(26/255.0) green:(169/255.0) blue:(213/255.0) alpha:1.0f];

    commString = [NSString stringWithFormat:@"Ref. ID : %@", propList.refIDOfFlat];
    addressLabel.text = propList.addressOfFlat;
    NSLog(@"data is %@", propList.addressOfFlat);
    typeLabel.text = propList.typeOfFlat;
    commString = [NSString stringWithFormat:@"%@ Sq. Meter.", propList.areaOfFlat];
    areaLabel.text = commString;
    commString = [NSString stringWithFormat:@"%@ KWD", propList.amountOfFlat];
    priceLabel.text = commString;

    myImageview.image = [UIImage imageNamed:propList.imageName];

    [cell.contentView  addSubview:myImageview];
    [myImageview release];

    [cell.contentView  addSubview:addressLabel];
    [addressLabel release];

    [cell.contentView  addSubview:typeLabel];
    [typeLabel release];

    [cell.contentView  addSubview:areaLabel];
    [areaLabel release];

    [cell.contentView  addSubview:priceLabel];
    [priceLabel release];

    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return NO;
}

- (void)dealloc {
    [headerLabel release];
    [secondImage release];
    [myTableView release];
    [super dealloc];
}
@end

これでアプリを実行すると、下の画面が表示されます。これで問題ありません。

画像をアップロードする権利がないので、リンクを提供しています

画像1

下にスクロールすると、この画面が表示されます。

画像2

最後の行を見ると、テキストが混在しています。:(なぜこれが起こっているのかわかりませんか?

上にスクロールすると、下の画像に示すようにテキストが混合されます。

画像3

つまり、1つのラベルに2つのテキストが表示されます

ここで、建物のエントリをもう1つ追加しました。以下は、tableViewを上下にスクロールした後に取得するものです。

画像4

つまり、1つのラベルに2つのテキストが表示されます

私の質問は、なぜこれがこの場合に起こっているのかということです。

4

4 に答える 4

1

コードに次のようにコメントするだけです。

//if (cell==nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"];
//}

問題が解決したかどうかを確認します。

于 2013-01-21T07:24:01.220 に答える
1

私があなたのコードから見たものから、あなたは前のセルを再利用していますが、毎回すべてのラベルと画像ビューを割り当て/初期化しています。これは良いアプローチではなく、このすべての問題を引き起こしています。

識別子が異なる2つのセルを作成し、後で次のように更新された値を割り当てるだけで、これらのサブビューを毎回作成する必要はありません。

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

NSString *commString;

NSString *myLanguage = localize(@"myLanguage");

UIImageView *myImageview;
UILabel *addressLabel;
UILabel *typeLabel;
UILabel *areaLabel;
UILabel *priceLabel;

NSString *cellIdentifier = [myLanguage isEqualToString:@"en"]?@"EngCell":@"ARCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

    if ([myLanguage isEqualToString:@"en"]) {
        myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(4, 4, 75, 65)];
        addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(87, 9, 223, 21)];
        typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(87, 32, 223, 21)];
        areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(87, 55, 223, 21)];
        priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 74, 110, 21)];
    } else {
        myImageview = [[UIImageView alloc] initWithFrame:CGRectMake(240, 004, 75, 65)];
        addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, 9, 223, 21)];
        typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, 32, 223, 21)];
        areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(11, 55, 223, 21)];
        priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 74, 110, 21)];
        addressLabel.textAlignment = NSTextAlignmentRight;
        typeLabel.textAlignment = NSTextAlignmentRight;
        areaLabel.textAlignment = NSTextAlignmentRight;
        priceLabel.textAlignment = NSTextAlignmentRight;
    }

    [addressLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 13.0]];
    [typeLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 13.0]];
    [areaLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 13.0]];
    [priceLabel setFont:[UIFont fontWithName:localize(@"myFont") size: 15.0]];
    [priceLabel setFont:[UIFont boldSystemFontOfSize:15.0]];


    addressLabel.backgroundColor = [UIColor clearColor];
    typeLabel.backgroundColor = [UIColor clearColor];
    areaLabel.backgroundColor = [UIColor clearColor];
    priceLabel.backgroundColor = [UIColor clearColor];

    myImageview.tag = 10;
    addressLabel.tag = 11;
    typeLabel.tag = 12;
    areaLabel.tag = 13;
    priceLabel.tag = 14;

    addressLabel.textColor = [UIColor colorWithRed:(246/255.0) green:(230/255.0) blue:(74/255.0) alpha:1.0f];
    typeLabel.textColor = [UIColor colorWithRed:(246/255.0) green:(230/255.0) blue:(74/255.0) alpha:1.0f];
    areaLabel.textColor = [UIColor colorWithRed:(246/255.0) green:(230/255.0) blue:(74/255.0) alpha:1.0f];
    priceLabel.textColor = [UIColor colorWithRed:(26/255.0) green:(169/255.0) blue:(213/255.0) alpha:1.0f];

    [cell.contentView  addSubview:myImageview];
    [myImageview release];

    [cell.contentView  addSubview:addressLabel];
    [addressLabel release];

    [cell.contentView  addSubview:typeLabel];
    [typeLabel release];

    [cell.contentView  addSubview:areaLabel];
    [areaLabel release];

    [cell.contentView  addSubview:priceLabel];
    [priceLabel release];
}

myImageview = [cell viewWithTag:10];
addressLabel = [cell viewWithTag:11];
typeLabel = [cell viewWithTag:12];
areaLabel = [cell viewWithTag:13];
priceLabel = [cell viewWithTag:14];

ListOfProperty *propList = [propListFinal objectAtIndex:indexPath.row];

commString = [NSString stringWithFormat:@"Ref. ID : %@", propList.refIDOfFlat];
addressLabel.text = propList.addressOfFlat;
NSLog(@"data is %@", propList.addressOfFlat);
typeLabel.text = propList.typeOfFlat;
commString = [NSString stringWithFormat:@"%@ Sq. Meter.", propList.areaOfFlat];
areaLabel.text = commString;
commString = [NSString stringWithFormat:@"%@ KWD", propList.amountOfFlat];
priceLabel.text = commString;

myImageview.image = [UIImage imageNamed:propList.imageName];

return cell;

}

よろしく、

于 2013-01-21T07:31:16.080 に答える
0

次のコードを使用します。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
    }
于 2013-01-21T07:29:51.347 に答える
0

その再利用性の問題。コードでも何度も直面したので、カスタムセルが好きです。常に再利用性の問題を解決します。したがって、カスタムテーブルビューセルを作成してみてから、レイアウトサブビューを変更します。

于 2013-01-21T08:53:15.473 に答える