UITableviewcell加载数据问题

问题描述 投票:1回答:2

我从feed解析了XML数据。我将所有数据存储在NSArray中。我使用UITableviewUILable中加载了图像和标题。但是当我使用滚动时,文本将按如下方式折叠。

我的代码如下......

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }           
    CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
    valueField = [[UILabel alloc] initWithFrame:frame];
    [valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    valueField.tag = 111;
    valueField.font=[UIFont fontWithName:@"Helvetica" size:13.0];
    valueField.textAlignment = UITextAlignmentLeft;
    valueField.lineBreakMode=UILineBreakModeWordWrap;
    valueField.numberOfLines = 0;
    valueField.textColor = [UIColor whiteColor];
    valueField.text=[title1 objectAtIndex:indexPath.row];
    valueField.highlightedTextColor = [UIColor whiteColor];
    valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    valueField.adjustsFontSizeToFitWidth = YES;
    valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    [cell.contentView addSubview:valueField];
    [valueField release];
    UIImage *patternImage = [UIImage imageNamed:@"bg.jpg"];
    UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
    backView.backgroundColor = [UIColor colorWithPatternImage: patternImage];
    cell.backgroundView = backView;
    NSString *image1 =[images objectAtIndex:indexPath.row];
    NSLog(@"Images  .....   = %@",image1);
    NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:indexPath.row]]];
    UIImage *myimage = [[UIImage alloc] initWithData:mydata];
    UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: myimage ];
    imageView.frame = CGRectMake(0.0f, 00.0f, 60.0f, 63.0f); 
    [cell.contentView addSubview:imageView];
    //cell.imageView.image = myimage;
    return cell;
}

我不知道如何解决这个问题。

objective-c ios cocoa-touch
2个回答
2
投票

每次重新加载单元格时,都会添加UILabel。

做点什么

if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    CGRect frame = CGRectMake(70.0, 00.0, 250.0, 55);
    valueField = [[UILabel alloc] initWithFrame:frame];
    [valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    valueField.tag = 111;
    valueField.font=[UIFont fontWithName:@"Helvetica" size:13.0];
    valueField.textAlignment = UITextAlignmentLeft;
    valueField.lineBreakMode=UILineBreakModeWordWrap;
    valueField.numberOfLines = 0;
    valueField.textColor = [UIColor whiteColor];
    valueField.highlightedTextColor = [UIColor whiteColor];
    valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    valueField.adjustsFontSizeToFitWidth = YES;
    valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
    [cell.contentView addSubview:valueField];
    [valueField release];
}
valueField.text=[title1 objectAtIndex:indexPath.row];

0
投票

更具体地说,由于您要将单元格出列,当您调用cellForRowAtIndexPath时,它可能会返回一个已创建的单元格。

该单元格已经创建了一个标签。

因此,只有当dequeueReusableCellWithIdentifier返回nil值时,才能在单元格中创建和设置自定义视图的所有魔力

© www.soinside.com 2019 - 2024. All rights reserved.