0

uitableviewに表示される文字列の配列があります。ユーザーが並べ替えボタンをタップすると、配列が並べ替えられ、[tableview reloaddata] を使用します。新しいソートされたコンテンツがテーブルに表示されるようにします。しかし、特定のセルを選択すると、セルに2つのテキストが重なって表示されます。新しいソートされたテキストと、そのセルに以前に存在していたテキストです。

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

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

}
4

2 に答える 2

1

コードに問題があります。再利用可能なセルを使用していますが、問題はセル内のビューを再使用していないことです。特に、timeLabel. セルを使用するたびに新しい timeLabel を作成しています。セルを再利用するときは、セルに adicional ラベルを追加します。これが、テキストが重なり合う理由として考えられます。

ラベルを再利用するには、UILabel に TAG 番号を設定する必要があります。新しい 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 を使用します。幸運を!

于 2012-04-12T12:33:18.097 に答える
0

カスタマイズされたセルが必要で、これが発生するたびにセルに新しいサブビューを配置するだけなので、別のペン先で作成されたカスタマイズされたセルを使用し、それを使用してデータを表示する必要があります

新しいペン先を作成して新しいペン先からロードするだけで、適切なデータが得られます

または、セルのcontentViewにサブビューがあるかどうかを確認してから、最初にそれらを削除してから、新しく作成されたサブビューを追加できます

于 2012-04-12T12:18:09.233 に答える