2011-11-06 50 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