简体   繁体   English

平滑的tableview滚动,每个单元格中有多个标签和webview?

[英]Smooth tableview scrolling with multiple label and a webview in each cell?

How to make tableview scroll smooth...I am inserting 8 label and a webview in each cell of tableview due to this tableview scrolling is not smooth... below is my code 如何使tableview滚动平滑...由于此tableview滚动不平滑,因此我在tableview的每个单元格中插入了8个标签和一个webview ...下面是我的代码

{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
        arr =[appDelegate.recentlyaddedarray objectAtIndex:indexPath.row];

        NSString *cellValue = [arr objectForKey: @"title"]; 
    #if USE_CUSTOM_DRAWING
        const NSInteger TOP_LABEL_TAG = 1001;

        UILabel *topLabel;


#endif

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell =[[[UITableViewCell alloc]  initWithFrame:CGRectZero  reuseIdentifier:CellIdentifier] autorelease];
    UIImage *indicatorImage = [UIImage imageNamed:@"arrow.png"];
    cell.accessoryView =[[[UIImageView alloc]  initWithImage:indicatorImage] autorelease];


#if USE_CUSTOM_DRAWING


    const CGFloat LABEL_HEIGHT =100;
    arr =[appDelegate.recentlyaddedarray objectAtIndex:indexPath.row];
    appDelegate.totalComments=[arr objectForKey:@"totalComments"];
    appDelegate.totalViews=[arr objectForKey: @"totalViews"];
    appDelegate.totalVotes=[arr objectForKey: @"totalVotes"];
    appDelegate.textfield=[arr objectForKey: @"title"];
    appDelegate.mytextview=[arr objectForKey: @"verse"];
    appDelegate.userid=[arr objectForKey: @"user_id"];
    appDelegate.added_date=[arr objectForKey: @"addedDate"];
    appDelegate.username=[arr objectForKey:@"user_name"];
    appDelegate.profilepic=[arr objectForKey:@"profile_pic"];
    NSLog(@"%@",appDelegate.profilepic);





    CGRect webFrame = CGRectMake(0.0, 0.0,80 ,80 );
    UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
    webView.userInteractionEnabled=FALSE;   
    [webView setBackgroundColor:[UIColor clearColor]];
    webView.opaque=NO;
    NSString *htmlTempString = [NSString stringWithFormat:@"<html><body><img src=\"%@\" width=50 height=60 /></body></html>",appDelegate.profilepic];
    NSString *htmlTempString = [NSString stringWithFormat:@"\n%@\n %@\n\n\n\n Votes(%@) Views(%@) Comments(%@)",cellValue,appDelegate.added_date,appDelegate.totalVotes,appDelegate.totalViews,appDelegate.totalComments];      
[webView loadHTMLString:htmlTempString baseURL:nil];
[cell.contentView addSubview:webView];
    [webView release];  







    topLabel = [[[UILabel alloc]  initWithFrame:  CGRectMake(70,0,240,LABEL_HEIGHT)] autorelease];
    [topLabel setNumberOfLines:-1];




    UILabel *Titlelable = [[[UILabel alloc]  initWithFrame:  CGRectMake(57,4,240,20)] autorelease];
    [cell.contentView addSubview:Titlelable];
    Titlelable.tag = TOP_LABEL_TAG;
    Titlelable.backgroundColor = [UIColor clearColor];
    Titlelable.textColor = [UIColor whiteColor];
    Titlelable.font=[UIFont fontWithName:@"Arial" size:16];
    Titlelable.text = [NSString stringWithFormat:@"\n\n\n\n%@\n",cellValue];



    UILabel *usernamelabel = [[[UILabel alloc]  initWithFrame:  CGRectMake(68,20,240,20)] autorelease];
    [cell.contentView addSubview:usernamelabel];
    usernamelabel.tag = TOP_LABEL_TAG;
    usernamelabel.backgroundColor = [UIColor clearColor];
    usernamelabel.textColor = [UIColor whiteColor];
    usernamelabel.font=[UIFont fontWithName:@"Arial" size:10];
    usernamelabel.text = [NSString stringWithFormat:@"\nBy %@\n",appDelegate.username];





    UILabel *Timelabel = [[[UILabel alloc]  initWithFrame:  CGRectMake(57,40,240,20)] autorelease];
    [cell.contentView addSubview:Timelabel];
    Timelabel.tag = TOP_LABEL_TAG;
    Timelabel.backgroundColor = [UIColor clearColor];
    Timelabel.textColor = [UIColor greenColor];
    Timelabel.font=[UIFont fontWithName:@"Arial" size:12];
    Timelabel.text = [NSString stringWithFormat:@"\n\n\n\n%@\n",appDelegate.added_date];




    UILabel *votelable = [[[UILabel alloc]  initWithFrame:  CGRectMake(101,56,240,20)] autorelease];
    [cell.contentView addSubview:votelable];
    votelable.tag = TOP_LABEL_TAG;
    votelable.backgroundColor = [UIColor clearColor];
    votelable.textColor = [UIColor whiteColor];
    votelable.font=[UIFont fontWithName:@"Arial" size:12];
    votelable.text = [NSString stringWithFormat:@"\n%@\n",appDelegate.totalVotes];




    UILabel *viewlable = [[[UILabel alloc]  initWithFrame:  CGRectMake(155,56,240,20)] autorelease];
    [cell.contentView addSubview:viewlable];
    viewlable.tag = TOP_LABEL_TAG;
    viewlable.backgroundColor = [UIColor clearColor];
    viewlable.textColor = [UIColor whiteColor];
    viewlable.font=[UIFont fontWithName:@"Arial" size:12];
    viewlable.text = [NSString stringWithFormat:@"\n%@\n\n",appDelegate.totalViews];




    UILabel *commentlabel = [[[UILabel alloc]  initWithFrame:  CGRectMake(235,56,240,20)] autorelease];
    [cell.contentView addSubview:commentlabel];
    commentlabel.tag = TOP_LABEL_TAG;
    commentlabel.backgroundColor = [UIColor clearColor];
    commentlabel.textColor = [UIColor whiteColor];
    commentlabel.font=[UIFont fontWithName:@"Arial" size:12];
    commentlabel.text = [NSString stringWithFormat:@"\n\n%@\n",appDelegate.totalComments];





    [cell.contentView addSubview:topLabel];
    topLabel.tag = TOP_LABEL_TAG;
    topLabel.backgroundColor = [UIColor clearColor];
    topLabel.textColor = [UIColor lightGrayColor];
    topLabel.font=[UIFont fontWithName:@"Arial" size:12];

    UIImage *cellBack = [UIImage imageNamed:@"cell.png"];
    cell.backgroundView =[[[UIImageView alloc] initWithImage:cellBack] autorelease];
    topLabel.text = [NSString stringWithFormat:@"Votes(   ) Views(    ) Comments(   )"];
//  topLabel.text = [NSString stringWithFormat:@"\n%@\n\n\n\n Votes(%@) Views(%@) Comments(%@)",appDelegate.added_date,appDelegate.totalVotes,appDelegate.totalViews,appDelegate.totalComments];
//  topLabel.text = [NSString stringWithFormat:@"\n%@\n %@\n\n\n\n Votes(%@) Views(%@) Comments(%@)",cellValue,appDelegate.added_date,appDelegate.totalVotes,appDelegate.totalViews,appDelegate.totalComments]; 
#else
    cell.text = [NSString stringWithFormat:@"Cell at row %ld.", [indexPath row]];



#endif

    return cell;

    }

I don't want to offend you but you should get more practice with basic topics. 我不想冒犯您,但您应该在基本主题上进行更多练习。

For example this code: 例如这段代码:

NSMutableArray *arr=[[NSMutableArray alloc]init];
arr =[appDelegate.recentlyaddedarray objectAtIndex:indexPath.row];

produces a leak, because you assign a new object to a variable holding an object without releasing the old one. 会产生泄漏,因为您将一个新对象分配给一个保存对象的变量而没有释放旧对象。

This code: 这段代码:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell =[[[UITableViewCell alloc]  initWithFrame:CGRectZero  reuseIdentifier:CellIdentifier] autorelease];

is useless, because you do basically the same like in the first snippet. 是没有用的,因为您所做的基本上与第一个代码片段相同。 You don't reuse your cells, that's why your scrolling isn't smooth. 您不会重复使用单元格,这就是为什么滚动不流畅的原因。

Change it to something like this: 将其更改为以下内容:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell =[[[UITableViewCell alloc]  initWithFrame:CGRectZero  reuseIdentifier:CellIdentifier] autorelease];
    // everything else
    // example:

    UILabel *titleLabel = [[[UILabel alloc]  initWithFrame:  CGRectMake(57,4,240,20)] autorelease];
    [cell.contentView addSubview:titleLabel];
    titleLabel.tag = TOP_LABEL_TAG;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font=[UIFont fontWithName:@"Arial" size:16];

}
// set values
// example:
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:TOP_LABEL_TAG];
titleLabel.text = [NSString stringWithFormat:@"\n\n\n\n%@\n",cellValue];

Instead of // everything else you put all your calls that add new views to the cell. 代替// everything else您放置了将所有新视图添加到单元格的所有调用。

Instead of // set values you use the tags you set earlier to change the attributes of the views you have added. 而不是// set values而是使用先前设置的标记来更改已添加的视图的属性。

Et voila, smooth scrolling. 瞧,平滑滚动。

If you've done this you should get rid of that useless monster: 如果这样做,您应该摆脱那个无用的怪物:

const CGFloat LABEL_HEIGHT =100;
arr =[appDelegate.recentlyaddedarray objectAtIndex:indexPath.row];
appDelegate.totalComments=[arr objectForKey:@"totalComments"];
/../
appDelegate.profilepic=[arr objectForKey:@"profile_pic"];
NSLog(@"%@",appDelegate.profilepic);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM