TableView在iPhone中滚动,但在Simulator中运行良好

时间:2011-02-17 12:26:14

标签: iphone objective-c

我一直在iPhone中开发一个应用程序,我正在为表格视图制作自定义单元格。 以下是cellForRowAtIndexPath的代码。

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

// Configure the cell...

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];


    NSDictionary *CurrentCommentRow = [comments objectAtIndex:indexPath.row];
    NSString *Comment = [[CurrentCommentRow objectForKey:@"Comment"] objectForKey:@"text"];
    NSString *FirstName = [[CurrentCommentRow objectForKey:@"F_Name"] objectForKey:@"text"];
    NSString *LastName = [[CurrentCommentRow objectForKey:@"L_Name"] objectForKey:@"text"];
    NSString *Date = [[CurrentCommentRow objectForKey:@"date"] objectForKey:@"text"];
    NSString *Time = [[CurrentCommentRow objectForKey:@"Time"] objectForKey:@"text"];
    NSString *UserImagePath = [[CurrentCommentRow objectForKey:@"UserImage"] objectForKey:@"text"];
    NSString *TotalComments = (NSString*)[[CurrentCommentRow objectForKey:@"TotalComment"] objectForKey:@"text"];
    NSString *imageRelativePath = [UserImagePath substringFromIndex:[UserImagePath rangeOfString:@"/" options:NSBackwardsSearch].location];


    UIView *CommentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 297, 140)];

    NSString* imageURL = [NSString stringWithFormat:@"http://primehangout.com/Images/IPhone%@",imageRelativePath];
    NSURL* url = [NSURL URLWithString:imageURL];        
    HJManagedImageV *managedImage = [[HJManagedImageV alloc] initWithFrame:CGRectMake(10, 18, 56, 56)];
    managedImage.url = url;
    [self.objManager manage:managedImage];
    [CommentView addSubview:managedImage];

    UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(82, 18, 180, 11)];
    lblName.font = [UIFont boldSystemFontOfSize:12];
    lblName.textColor = [UIColor colorWithRed:(5.0/255.0) green:(102.0/255.0) blue:(202.0/255.0) alpha:255];
    lblName.text = [NSString stringWithFormat:@"%@:",FirstName];        
    [CommentView addSubview:lblName];
    [lblName release];

    UITextView *txtViewComment = [[UITextView alloc] initWithFrame:CGRectMake(73, 34, 180, 40)];
    txtViewComment.contentInset = UIEdgeInsetsMake(-10,0,0,0);
    [txtViewComment setEditable:FALSE];
    txtViewComment.font = [UIFont fontWithName:@"Arial" size:11];
    txtViewComment.text = Comment;
    txtViewComment.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
    [CommentView addSubview:txtViewComment];
    [txtViewComment release];


    UILabel *lblDateTime = [[UILabel alloc] initWithFrame:CGRectMake(82, 79, 180, 20)];
    lblDateTime.font = [UIFont fontWithName:@"Arial" size:10];
    lblDateTime.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
    lblDateTime.text = [NSString stringWithFormat:@"%@ %@",Date,Time];      
    [CommentView addSubview:lblDateTime];
    [lblDateTime release];

    UILabel *lblTotalComments = [[UILabel alloc] initWithFrame:CGRectMake(82, 95, 80, 20)];
    lblTotalComments.font = [UIFont fontWithName:@"Arial" size:11];
    lblTotalComments.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
    lblTotalComments.text = [NSString stringWithFormat:@"%@ Comments",TotalComments];       
    [CommentView addSubview:lblTotalComments];
    [lblTotalComments release];



    if ([TotalComments integerValue]>0) 
    {
        UIButton *btnViewAll = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnViewAll setFrame:CGRectMake(140, 82, 70, 45)];
        [btnViewAll setImage:[UIImage imageNamed:@"view_all_button_bg.png"] forState:UIControlStateNormal];
        //[lblViewAll setTitle:@"view all" forState:UIControlStateNormal];
        [btnViewAll addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [btnViewAll setTag:(150 + indexPath.row)];
        [CommentView addSubview:btnViewAll];                    
    }
    else {
        [lblTotalComments setFrame:CGRectMake(82, 95, 150, 20)];
    }


    UIButton *btnAddComment = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnAddComment setFrame:CGRectMake(200, 82, 70, 45)];
    [btnAddComment setImage:[UIImage imageNamed:@"add_comment.png"] forState:UIControlStateNormal];
    [btnAddComment addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [btnAddComment setTag:(1050 + indexPath.row)];
    [CommentView addSubview:btnAddComment];

    [cell.contentView addSubview: CommentView];

return cell;

}

该代码在iPhone模拟器中运行良好,但桌面视图在真正的iPhone中滚动。

据我所知,重用可以解决这个问题,我也尝试过以下代码:

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


static NSString *CellIdentifier = @"Cell";
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


  // Configure the cell...




    NSDictionary *CurrentCommentRow = [comments objectAtIndex:indexPath.row];
    NSString *Comment = [[CurrentCommentRow objectForKey:@"Comment"] objectForKey:@"text"];
    NSString *FirstName = [[CurrentCommentRow objectForKey:@"F_Name"] objectForKey:@"text"];
    NSString *LastName = [[CurrentCommentRow objectForKey:@"L_Name"] objectForKey:@"text"];
    NSString *Date = [[CurrentCommentRow objectForKey:@"date"] objectForKey:@"text"];
    NSString *Time = [[CurrentCommentRow objectForKey:@"Time"] objectForKey:@"text"];
    NSString *UserImagePath = [[CurrentCommentRow objectForKey:@"UserImage"] objectForKey:@"text"];
    NSString *TotalComments = (NSString*)[[CurrentCommentRow objectForKey:@"TotalComment"] objectForKey:@"text"];
    NSString *imageRelativePath = [UserImagePath substringFromIndex:[UserImagePath rangeOfString:@"/" options:NSBackwardsSearch].location];


    UIView *CommentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 297, 140)];

    NSString* imageURL = [NSString stringWithFormat:@"http://primehangout.com/Images/IPhone%@",imageRelativePath];
    NSURL* url = [NSURL URLWithString:imageURL];        
    HJManagedImageV *managedImage = [[HJManagedImageV alloc] initWithFrame:CGRectMake(10, 18, 56, 56)];
    managedImage.url = url;
    [self.objManager manage:managedImage];
    [CommentView addSubview:managedImage];

    UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(82, 18, 180, 11)];
    lblName.font = [UIFont boldSystemFontOfSize:12];
    lblName.textColor = [UIColor colorWithRed:(5.0/255.0) green:(102.0/255.0) blue:(202.0/255.0) alpha:255];
    lblName.text = [NSString stringWithFormat:@"%@:",FirstName];        
    [CommentView addSubview:lblName];
    [lblName release];

    UITextView *txtViewComment = [[UITextView alloc] initWithFrame:CGRectMake(73, 34, 180, 40)];
    txtViewComment.contentInset = UIEdgeInsetsMake(-10,0,0,0);
    [txtViewComment setEditable:FALSE];
    txtViewComment.font = [UIFont fontWithName:@"Arial" size:11];
    txtViewComment.text = Comment;
    txtViewComment.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
    [CommentView addSubview:txtViewComment];
    [txtViewComment release];


    UILabel *lblDateTime = [[UILabel alloc] initWithFrame:CGRectMake(82, 79, 180, 20)];
    lblDateTime.font = [UIFont fontWithName:@"Arial" size:10];
    lblDateTime.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
    lblDateTime.text = [NSString stringWithFormat:@"%@ %@",Date,Time];      
    [CommentView addSubview:lblDateTime];
    [lblDateTime release];

    UILabel *lblTotalComments = [[UILabel alloc] initWithFrame:CGRectMake(82, 95, 80, 20)];
    lblTotalComments.font = [UIFont fontWithName:@"Arial" size:11];
    lblTotalComments.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
    lblTotalComments.text = [NSString stringWithFormat:@"%@ Comments",TotalComments];       
    [CommentView addSubview:lblTotalComments];
    [lblTotalComments release];



    if ([TotalComments integerValue]>0) 
    {
        UIButton *btnViewAll = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnViewAll setFrame:CGRectMake(140, 82, 70, 45)];
        [btnViewAll setImage:[UIImage imageNamed:@"view_all_button_bg.png"] forState:UIControlStateNormal];
        //[lblViewAll setTitle:@"view all" forState:UIControlStateNormal];
        [btnViewAll addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [btnViewAll setTag:(150 + indexPath.row)];
        [CommentView addSubview:btnViewAll];                    
    }
    else {
        [lblTotalComments setFrame:CGRectMake(82, 95, 150, 20)];
    }


    UIButton *btnAddComment = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnAddComment setFrame:CGRectMake(200, 82, 70, 45)];
    [btnAddComment setImage:[UIImage imageNamed:@"add_comment.png"] forState:UIControlStateNormal];
    [btnAddComment addTarget:self action:@selector(ViewAllBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [btnAddComment setTag:(1050 + indexPath.row)];
    [CommentView addSubview:btnAddComment];

    [cell.contentView addSubview: CommentView];

}



return cell;

}

在使用上面的代码之后,滚动变得平滑并且问题得到解决但是引发的新问题是重复内容被随机显示并且所有数据被误导为例如。第一个细胞显示3-4次,依此类推......

如果有什么不清楚的话随便问朋友。 :)

2 个答案:

答案 0 :(得分:1)

重用是要走的路,但你做错了。
当然,您必须在重复使用单元格后设置新值。

if (cell == nil) {
    // create new cell
    cell = ...
    UILabel *lblTotalComments = [[UILabel alloc] initWithFrame:CGRectMake(82, 95, 80, 20)];
    lblTotalComments.tag = 192;
    lblTotalComments.font = [UIFont fontWithName:@"Arial" size:11];
    lblTotalComments.textColor = [UIColor colorWithRed:(75.0/255.0) green:(75.0/255.0) blue:(75.0/255.0) alpha:255];
    [CommentView addSubview:lblTotalComments];
    [lblTotalComments release];

}
// configure cell
UILabel *lblTotalComments = (UILabel *)[cell viewWithTag:192];
lblTotalComments.text = [NSString stringWithFormat:@"%@ Comments",TotalComments];       

你应该知道如何实现这个。

答案 1 :(得分:1)

您每次都是从互联网上获取图像吗?为什么不将它存储在imagecollection中?