2011-11-06 54 views
0

请参阅遇到问题在显示的UITableView

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

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
UITableViewCell *cell = [tableView 
         dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:SimpleTableIdentifier] autorelease]; 




UILabel *playerHeading=[[UILabel alloc ]initWithFrame:CGRectMake(5.0f, 10.0f, 70.0f, 30.0f)]; 
[playerHeading setBackgroundColor:[UIColor clearColor]]; 


NSString *str=[NSString stringWithFormat:@"Player %i",indexPath.row]; 
[playerHeading setText:str]; 
[playerHeading setFont:[UIFont boldSystemFontOfSize:15]]; 

if(indexPath.section%2==0){ 
    playerHeading.textColor=[UIColor redColor]; 
} 
else { 
    playerHeading.textColor=[UIColor blueColor]; 

} 

UITextField *txt=[[UITextField alloc]initWithFrame:CGRectMake(80.0f, 13.0f, 170.0f, 30.0f)]; 
[txt setBackgroundColor:[UIColor clearColor]]; 


//[txt setBackgroundColor:[UIColor whiteColor]]; 
[txt setTextColor:[UIColor grayColor]]; 
txt.delegate=self; 
//[txt setBorderStyle:UIBorderStyle ] 
[txt setText:@"Enter Player name"]; 
//txt.layer.cornerRadius=8.0f; 
    // txt.layer.masksToBounds=YES; 
//txt.layer.borderColor=[[UIColor redColor]CGColor]; 
    // txt.layer.borderWidth= 1.0f; 

[cell.contentView addSubview:txt]; 

[cell.contentView addSubview:playerHeading]; 

[cell.contentView setAlpha:0.8f]; 
//ce]ll.textLabel.text = [listData objectAtIndex:row]; 
} 
return cell; 

}

我希望按顺序显示的数字,如球员0,玩家1,玩家2 ... 但我滚动表我以随机格式获得数字,如播放器1,播放器0,播放器3 请帮助如何解决此问题。

回答

1

您将始终将标签添加为新的子视图 - 这意味着您将标签上方的标签标签以上,这是一个浪费内存以及潜在的显示问题的来源。 。

当你创建你的细胞(内if (cell == nil)创建,并在该点添加子视图,并指定每一个标签,然后配置他们所有的外循环的例子只是你playerHeading标签:

UILabel *playerHeading; 

if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    playerHeading=[[UILabel alloc ]initWithFrame:CGRectMake(5.0f, 10.0f, 70.0f, 30.0f)]; 
    [playerHeading setBackgroundColor:[UIColor clearColor]]; 
    [playerHeading setFont:[UIFont boldSystemFontOfSize:15]]; 
    [playerHeading setTag:1]; 
    [cell.contentView addSubview:playerHeading]; 
} 
else 
{ 
    playerHeading = [cell.contentView viewWithTag:1]; 
} 

playerHeading.text = [NSString stringWithFormat:@"Player %i",indexPath.row]; 

您的表中似乎有多个部分(您在indexPath.section上有一个if声明),所以您的播放器编号将从上面的代码的每个部分的0开始重新开始

+0

但我有动态使用的TextField,如果我尝试添加他们如何可以获取它们的值是一个数组,数组将有8个TextField的最大值:虽然我使用了100 – androider

+0

你会做同样的你的文本框但会将字符串值(而不是字段)保留在数组中,并每次更新。编辑文本字段时,获取正在编辑的行的索引路径,以便知道要更新的数组的哪个元素。使用 – jrturton

0

尝试关闭{ }之前的if(cell == nil)如下:

if (cell == nil) { 
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:SimpleTableIdentifier] autorelease]; 
} 
+0

但它创建数据重复 – androider