2009-08-08 76 views
0

我目前的问题相关链接如下。 UICell问题iPhone滚动表查看


http://www.freeimagehosting.net/image.php?a65dae5f4a.png>http://www.freeimagehosting.net/uploads/th.a65dae5f4a.png alt =“FreeImageHosting.net提供免费图片托管服务”>

[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边界= 0中高音=“Free Image Hosting”>

[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。它可能会减慢你的应用程序&它不是正确的解决方案,因为在我的应用程序中,我有成千上万的学生&,如果我创建每个单元格时滚动我的应用程序可能会挂起。应该是什么解决方案?帮我......

回答

1

我认为问题在于,当单元格被回收时,您在已添加的子视图顶部添加子视图。

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

参考:An Apple doc on table view cells

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

完美地工作,我也可以在我的单元格中使用我的自定义内容视图。