UICell问题iPhone同时滚动tableView

时间:2009-08-08 18:02:43

标签: iphone

我当前的问题相关链接是以下之一。

<小时/> http://www.freeimagehosting.net/image.php?a65dae5f4a.png>http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png alt =“FreeImageHosting.net免费图片托管”&gt;

[URL = http://www.freeimagehosting.net/image.php?a65dae5f4a.png][img]http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png[/img][/url]

[URL = http://www.freeimagehosting.net/image.php?a65dae5f4a.png][img=http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png][/url]

http://www.freeimagehosting.net/>http://www.freeimagehosting.net/uploads/a65dae5f4a.png border = 0 alt =“免费图片托管”&gt;

[URL = http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/a65dae5f4a.png[/img][/url]

[URL = http://www.freeimagehosting.net/][img=http://www.freeimagehosting.net/uploads/a65dae5f4a.png][/url]


我在表视图中实现了以下代码,

enter code here

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

// customizing cell
student *t=[studentsList objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor lightGrayColor];
CGRect label1Frame=CGRectMake(10, 10, 290, 25);
CGRect label2Frame=CGRectMake(10, 33, 290, 25);
CGRect label3Frame=CGRectMake(10, 56, 290, 25);
UILabel *lbltmp;
lbltmp=[[UILabel alloc] initWithFrame:label1Frame];  // name
lbltmp.font=[UIFont boldSystemFontOfSize:15];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.textColor=[UIColor blackColor];
lbltmp.text=t.stuName;
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label2Frame]; // roll no & address
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%i - %@",t.stuNo,t.stuAddress];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label3Frame]; // city & pin
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%@ - %@",t.stuCity,t.stuPin];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
//----------------------------------
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
return cell;

}

如果我按上述方法实施代码,则会出现问题,如上图所示。

如果我实现以下代码,则不会出现问题。

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

// customizing cell
student *t=[studentsList objectAtIndex:indexPath.row];
cell.backgroundColor=[UIColor lightGrayColor];
CGRect label1Frame=CGRectMake(10, 10, 290, 25);
CGRect label2Frame=CGRectMake(10, 33, 290, 25);
CGRect label3Frame=CGRectMake(10, 56, 290, 25);
UILabel *lbltmp;
lbltmp=[[UILabel alloc] initWithFrame:label1Frame];  // name
lbltmp.font=[UIFont boldSystemFontOfSize:15];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.textColor=[UIColor blackColor];
lbltmp.text=t.stuName;
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label2Frame]; // roll no & address
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%i - %@",t.stuNo,t.stuAddress];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];

lbltmp=[[UILabel alloc] initWithFrame:label3Frame]; // city & pin
lbltmp.font=[UIFont systemFontOfSize:13];
lbltmp.backgroundColor=[UIColor clearColor];
lbltmp.text=[NSString stringWithFormat:@"%@ - %@",t.stuCity,t.stuPin];
lbltmp.textColor=[UIColor grayColor];
[cell.contentView addSubview:lbltmp];
[lbltmp release];
//----------------------------------
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
}
return cell;

}

实现上述代码意味着在用户滚动tableView时重新创建每个表格单元格。它可能会降低您的应用程序速度这不是正确的解决方案,

因为在我的申请中,我有成千上万的学生和如果我创建每个单元格而滚动我的应用程序可能会挂起应该是什么解决方案?帮助我......

2 个答案:

答案 0 :(得分:1)

我认为问题在于,当细胞被回收时,您会在已添加的子视图之上添加子视图。

解决此问题的一种方法:子类UITableViewCell并为您要更改的文本指定UILabel s。然后,当单元格被回收时,您只需访问这些属性,而不是重新添加新的子视图。

参考:An Apple doc on table view cells

答案 1 :(得分:0)

我认为您的问题的解决方案如下:

对您的tableViewCotroller

实施以下方法
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
{
}

例如我已实施以下

-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier
{
    CGRect photoFrame=CGRectMake(5, 5, 60, 60);
    CGRect label1Frame=CGRectMake(90, 10, 290, 25);
    CGRect label2Frame=CGRectMake(90, 30, 290, 25);
    CGRect label3Frame=CGRectMake(90, 50, 290, 25);

    UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 300, 80) reuseIdentifier:cellIdentifier] autorelease];

    UILabel *tmp;
    tmp=[[UILabel alloc] initWithFrame:label1Frame];
    tmp.tag=1;
    tmp.textColor=[UIColor blackColor];
    tmp.font=[UIFont boldSystemFontOfSize:15];
    [cell.contentView addSubview:tmp];
    [tmp release];

    tmp=[[UILabel alloc] initWithFrame:label2Frame];
    tmp.tag=2;
    tmp.adjustsFontSizeToFitWidth=0;
    tmp.textColor=[UIColor grayColor];
    tmp.font=[UIFont systemFontOfSize:13];
    [cell.contentView addSubview:tmp];
    [tmp release];

    tmp=[[UILabel alloc] initWithFrame:label3Frame];
    tmp.tag=3;
    tmp.adjustsFontSizeToFitWidth=0;
    tmp.textColor=[UIColor grayColor];
    tmp.font=[UIFont systemFontOfSize:13];
    [cell.contentView addSubview:tmp];
    [tmp release];

    UIImageView *imgView=[[UIImageView alloc]initWithFrame:photoFrame];
    imgView.tag=4;
    [cell.contentView addSubview:imgView];
    [imgView release];

    return cell;
}

我还调用了上述方法表单cellForRowAtIndexPath

例如

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [self getCellContentView:CellIdentifier];
    }
    // customizing cell
    student *t=[studentsList objectAtIndex:indexPath.row];
    cell.backgroundColor=[UIColor lightGrayColor];

    UILabel *lb1=(UILabel*)[cell viewWithTag:1];
    UILabel *lb2=(UILabel*)[cell viewWithTag:2];
    UILabel *lb3=(UILabel*)[cell viewWithTag:3];

    NSString *dtlOne=[NSString stringWithFormat:@"Enr.No - %i, %@",t.stuNo,t.stuAddress];
    NSString *dtlTwo=[NSString stringWithFormat:@"%@ - %@.",t.stuCity,t.stuPin];
    lb1.text=t.stuName;
    lb2.text=dtlOne;
    lb3.text=dtlTwo;    

    cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

    return cell;
}

效果很好,我也可以在单元格中使用自定义内容视图。