我有和在uitableview中顯示的字符串數組。當用戶點擊排序按鈕數組排序,然後我使用[tableview reloaddata]。以便新的排序內容顯示在表格中。但是當我選擇特定的單元格時,單元格會顯示兩個相互重疊的文本,新的排序文本和之前存在於該單元格中的文本。爲什麼會發生這種情況?單擊可用視圖單元格後獲取相同的數據
這是我的代碼來顯示單元格。
- (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];
}
謝謝,它的工作就像一個魅力..謝謝。 – 2012-04-12 12:38:58