UITableviewcell加载数据问题

时间:2010-11-11 13:38:06

标签: objective-c ios cocoa-touch

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

alt text

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

-(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;
}

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

2 个答案:

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

答案 1 :(得分:0)

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

该单元格已创建标签。

因此,只有在dequeueReusableCellWithIdentifier返回nil

时,才能在您的单元格中创建和设置自定义视图。