2012-01-25 5 views
0

4つのラベルでセルをカスタマイズしましたが、最初はうまく見えますが、スクロールして別の行のセルラベルを選択するとオーバーラップします。ここに私のコードです。どんな助力も高く評価されます。カスタムセルオーバーラップのラベル

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
static NSString *[email protected]"CellIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 

if (cell == nil) { cell = [[[UITableViewCell alloc] 
          initWithStyle:UITableViewCellStyleDefault 
          reuseIdentifier: CellIdentifier] autorelease]; 
} 
NSString *cellIconName = [[self cellIcon] objectAtIndex:[indexPath row]]; 
cellImage=[UIImage imageNamed:cellIconName]; 

cell.imageView.image=cellImage; 

UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, 140, 20)]; 
labelOne.text = [[NSString alloc]initWithFormat:@"%@",[dataList objectAtIndex:indexPath.row]]; 
[cell.contentView addSubview:labelOne]; 
[labelOne release]; 

UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)]; 
labelTwo.text = [[NSString alloc]initWithFormat:@"%@",[newPrice objectAtIndex:indexPath.row]]; 
[cell.contentView addSubview:labelTwo]; 
[labelTwo release]; 



UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(65, 30, 140, 20)]; 
label3.text=[[NSString alloc]initWithFormat:@"%@",[details objectAtIndex:indexPath.row]]; 
[cell.contentView addSubview:label3]; 
[label3 release]; 


UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(260, 30, 140, 20)]; 
label4.text=[[NSString alloc]initWithFormat:@"%@",[oldPrice objectAtIndex:indexPath.row]]; 
[cell.contentView addSubview:label4]; 
[label4 release]; 

return cell; 

}

答えて

4

間違いは、あなたがもし条件if(cell==nil)の外(ほかの部分)にラベルを割り当てられています。セルが最初に作成されるときにラベルを作成する必要があります。

コードを実行すると、コントロールはセルがnilであるか、値があるかどうかをチェックします。毎回elseパートにラベルを割り当てると(スクロール中も)毎回新しいラベルが作成されます。 if条件に割り当てると、セルごとにラベルが1回だけ割り当てられ、スクロールするときにテキスト値が更新されます。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"CellIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 

if (cell == nil) 
{ 
cell = [[[UITableViewCell alloc] 
                            initWithStyle:UITableViewCellStyleDefault 
                            reuseIdentifier: CellIdentifier] autorelease]; 

NSString *cellIconName = [[self cellIcon] objectAtIndex:[indexPath row]]; 
cellImage=[UIImage imageNamed:cellIconName]; 

cell.imageView.image=cellImage; 

UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(65, 10, 140, 20)]; 
labelOne.tag = 100; 
[cell.contentView addSubview:labelOne]; 
[labelOne release]; 

UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)]; 
labelTwo.tag = 101; 
[cell.contentView addSubview:labelTwo]; 
[labelTwo release]; 

} 

UILabel *labelOne = (UILabel *) [cell.contentView viewWithTag:100]; 
labelOne.text = [dataList objectAtIndex:indexPath.row]; 

UILabel *labelTwo = (UILabel *) [cell.contentView viewWithTag:101]; 
labelTwo.text = [newPrice objectAtIndex:indexPath.row]; 
  

return cell; 
} 
+0

このコードは、問題のコードよりも優れているなぜあなたはそれがだろう、説明してもらえまた、この次のコードスニペットを試すことができますこの答をアップヴォートの価値があるものにする。 – jrturton

+0

それは動作します...あなたは私のコードで何が間違っているか説明できますか? – NoviceDeveloper

+1

Mr.NoviceDeveloperとMr.Jrturton。間違いはif条件(if cell(== nil))の外で(else部分)のラベ​​ルを割り当てたことです。コードを実行すると、コントロールはセルがnilであるかどうか、または値があるかどうかをチェックします。毎回else部分にラベルを割り当てると(スクロール中も)、ラベルは配列から値を割り当てて取得します。 if条件で割り当てると、ラベルが各セルに割り当てられ、配列から値が取得されるだけです。私の貧しい英語のために申し訳ありません。明らかです?ありがとう。 –

1

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

for (UIView * subView in [cell.contentView subviews]) 
{ 
[subView removeFromSuperView]; 
} 

が、それはあなたを助けることを願っています:)の後