两个文本在UITableViewCell中出现在同一个标​​签上

时间:2013-01-21 07:19:50

标签: iphone objective-c uitableview ios6

我要做的是获取静态数据并在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

现在,当我运行应用程序时,我的屏幕下方很好。

由于我无权上传图片,因此我提供了链接

Image 1

现在当我向下滚动时,我有这个屏幕。

Image 2

当您查看最后一行时,文本会混合。 :(我不明白为什么会发生这种情况?

现在,当我向上滚动时,文本会再次混合,如下图所示。

Image 3

即。在一个标签上我看到两个文字

现在我又添加了一个建筑物入口,下面是我在向上和向下滚动tableView后得到的。

Image 4

即。在一个标签上我看到两个文字

我的问题是,为什么会发生这种情况?

4 个答案:

答案 0 :(得分:1)

在您的代码中注释如下:

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

检查问题是否已解决。

答案 1 :(得分: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;

}

此致

答案 2 :(得分:0)

使用以下代码。

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

答案 3 :(得分:0)

它的可重用性问题。我在代码中也遇到了很多次。所以我更喜欢自定义单元格。它总能解决可重用性问题。因此,尝试制作自定义tableview Cell.And然后修改布局子视图。