tableview滚动问题

时间:2011-06-26 12:39:54

标签: iphone xcode uitableview scroll

嗨,这是我的问题:

我从php脚本收到一些数据到我的tableview。但当我向下滚动并返回顶部时,我的数据是不可读的。如果我向下滚动,也一样。数据叠加

这是我的代码:

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [messageArray count];    

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    CGSize  textSize = { 260.0, 20000.0 };          

    NSMutableDictionary *dict = [messageArray objectAtIndex:indexPath.row];
    NSString *aMsg = [dict objectForKey:@"message"];

    CGSize size = [aMsg sizeWithFont:[UIFont systemFontOfSize:13.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
    size.height += 5;

    CGFloat height = size.height<36?36:size.height;

    return height;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    NSMutableDictionary *tempMsg = [messageArray objectAtIndex:indexPath.row];


    CGFloat result = 20;
    CGSize textSize = { 260.0, 20000.0 };


    NSMutableDictionary *dict = [messageArray objectAtIndex:indexPath.row];

    NSString *aMsg = [[dict objectForKey:@"message"]stringByReplacingOccurrencesOfString:@"\n" withString:@""];

    CGSize size = [aMsg sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];

    result = MAX(size.height -5.0, 25.0);


    // Configure the cell.

    CGRect frame = CGRectMake(15, 5, size.width , size.height);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];

    label.text = [tempMsg  objectForKey:@"message"];
    label.font = [UIFont systemFontOfSize:13];
    label.numberOfLines = 0;
    [label sizeToFit];
    label.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:label];

    [label release];




    [cell setFrame:CGRectMake(20.0, 0.0, size.width, result)];

    UIImage* balloon = [[UIImage imageNamed:@"grey.png"] stretchableImageWithLeftCapWidth:24  topCapHeight:15];
    UIImageView *newImage = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width+35, result+10)];
    UIView *newView =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cell.frame.size.width, cell.frame.size.height)];

    [newImage setImage:balloon];
    [newView addSubview:newImage];
    [cell setBackgroundView:newView];



    return cell;

}

1 个答案:

答案 0 :(得分:1)

每次请求tableview-cell时,您都要添加新的UILabelUIImageView。因此,在向下和向上滚动之后,Table-View-Cells包含多个标签和图像。

您应该将UITableViewCellUILabelUIImageView进行子类化。