1

What I am trying to do is taking the static data and show the data in UITableView. Below is what I have.

Note: : I am adding label and imageView in cell programatically as I have english and arabic version app.

//
//  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

Now when I run the app, I have below screen which is fine.

as I don't have right to upload image, I am providing link

Image 1

Now when I scroll down, I have this screen.

Image 2

When you look at last row, the text are mixed. :( I don't get why this is happening?

Now when I scroll up, again the text is mixed as shown in below image.

Image 3

i.e. on one label I see two text

Now I added one more entry of building and below is what I get after scrolling the tableView up and down.

Image 4

i.e. on one label I see two text

My question would be, why this is happening in this case?

4

4 回答 4

1

只需在您的代码中注释如下:

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

检查问题是否已解决。

于 2013-01-21T07:24:01.220 回答
1

从我从您的代码中看到的,您正在重用前一个单元格,但每次都分配/初始化所有标签和图像视图,这不是一个好方法并导致所有这些问题。

您不应该每次都创建这些子视图,只需创建两个具有不同标识符的单元格,然后再为它们分配更新的值,如下所示:

-(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 回答