English 中文(简体)
TableView在iPhone中无法滚动,但在模拟器中运行良好
原标题:TableView stuck at scrolling in iPhone but works well in Simulator

I have been developing one application in iPhone and I m making custom cells for a table view. Following is the code for 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次,以此类推。。。

If anything is unclear feel free to ask friends. :)

最佳回答

reusing is the way to go, but you are doing it wrong.
Of course you have to set the new values after you ve reused a cell.

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

你应该知道如何实现它。

问题回答

你是不是每次都从网上得到图片?你为什么不把它储存在图像收藏中呢?





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签