2012-04-12 6 views
0

私はuitableviewに表示される文字列を持っています。ユーザーがソートボタンをタップすると、配列がソートされ、次に[tableview reloaddata]が使用されます。新しいソートされたコンテンツがテーブルに表示されます。私が特定のセルを選択すると、セルには2つのテキストが重なって表示され、新しいソートされたテキストとそのセルに以前に存在したテキストが表示されます。uitableviewセルをクリックして同じデータを取得する

これは私のコードを表示するためのコードです。

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


} 


UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 0, 120, tableView.rowHeight)]; 
timeLabel.text = [[dataArray objectAtIndex:indexPath.row] time]; 
[cell.contentView addSubview:timeLabel]; 


return cell ; 
} 

これはソートのためのコードです。

-(void)sortByTime{ 

NSSortDescriptor *sortDescriptor; 
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"time" 
              ascending:NO] ; 
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
NSArray *sortedArray; 

dataArray = [sql getTableData]; // get data from sql file 

sortedArray = [dataArray sortedArrayUsingDescriptors:sortDescriptors]; 

dataArray = [[NSMutableArray alloc]initWithArray:sortedArray]; 

[dataTableView reloadData]; 

} 

答えて

1

あなたのコードに問題があるを追加し、最初にそれらを削除し、セルのcontentViewで任意のサブビューであるように、新しいペン先からそれをロードします。再利用可能なセルを使用していますが、問題はセル内のビューを再使用していないことです。分科会、タイムラベル。セルを使用するたびに新しいtimeLabelを作成しています。セルを再利用すると、セルにadicionalラベルが追加されます。これが、ovelappedテキストの考えられる理由です。

ラベルを再利用するには、UILabelのタグ番号を設定し、新しいuilabelを作成する前に、セルにすでにタグがあるかどうかを確認する必要があります。

UILabel * timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 0, 120, tableView.rowHeight)]; 
timeLabel.text = [[dataArray objectAtIndex:indexPath.row] time]; 
[cell.contentView addSubview:timeLabel]; 

私はコードを置き換えます

UILabel * timeLabel = [cell viewWithTag:55] 
if(!timeLabel) { 
    timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 0, 120, tableView.rowHeight)]; 
    timeLabel.tag = 55; 
    [cell.contentView addSubview:timeLabel]; 
} 

timeLabel.text = [[dataArray objectAtIndex:indexPath.row] time]; 

タグ番号の値は、私は一例として55を使用して、あなた次第です。がんばろう!

+0

ありがとうございます、それは魅力のように働いています。 –

0

あなたがカスタマイズしたセルをしたいとあなただけのセルで、これはあなたがあなたのデータを表示するために精ペン先に作成したカスタマイズされたセルを使用し、それを使用する必要が起こっされるたびに新しいサブビューを置くよう

ちょうど新しい作りますペン先とあなたが適切なデータ

を得たか確認することができますそこには、新しい作成したサブビュー

関連する問題