如何使tableview滚动顺利...我在tableview的每个单元格中插入8个标签和一个webview 由于这个tableview滚动不顺畅... 下面是我的代码
{
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;
}
答案 0 :(得分:2)
我不想冒犯你,但你应该更多地练习基本主题。
例如这段代码:
NSMutableArray *arr=[[NSMutableArray alloc]init];
arr =[appDelegate.recentlyaddedarray objectAtIndex:indexPath.row];
会产生泄漏,因为您将新对象分配给包含对象的变量而不释放旧对象。
此代码:
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell =[[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
没用,因为你在第一个片段中做的基本相同。你没有重复使用你的单元格,这就是你滚动不顺畅的原因。
将其更改为:
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];
而不是// everything else
,而是将所有调用添加到视图中的新视图。
您可以使用之前设置的标记来更改已添加的视图的属性,而不是// set values
。
Et瞧,顺畅滚动。
如果你这样做了,你应该摆脱那个无用的怪物:
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);