2011-06-26 61 views
0

嗨,這是我的問題:tableview滾動問題

我從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

你添加新的UILabel並且每次請求的tableview細胞的時間UIImageView。因此,在向下滾動並再次滾動之後,Table-View-Cells包含多個標籤和圖像。

您應該將UITableViewCell繼承爲UILabelUIImageView

+0

是的,看起來更好! – XcodeMania

+0

嗨,我已經放置了一個tableview查詢。你可以在下面的鏈接上檢查並回答這個問題。 http://stackoverflow.com/questions/8239543/tableview-is-not-showing-data –