2012-02-15 87 views
0

我已經爲四個標籤定製了uitableview單元格,並且還爲每個標籤保存了數據。 label1包含對象的名稱,標籤2包含價格標籤3,標籤4包含其他數據。現在有兩個按鈕,第一個按鈕點擊我想按照對象名稱的字母順序對錶格進行排序,第二個按鈕點擊我想根據價格排序..請指導我如何做到這一點..這是我的代碼。 。根據按鈕單擊字母或數字順序對UITableView進行排序

- (void)viewDidLoad { 
[super viewDidLoad]; 

details=[[NSMutableArray alloc]initWithObjects:@"dress",@"dress",@"dress",@"dress",@"dress",@"", 
      @"dress",@"",@"dress",nil]; 
newPrice=[[NSMutableArray alloc]initWithObjects:@"500",@"1000",@"5000",@"2100",@"1222",@"100",@"5223",@"465",@"3216",nil]; 
oldPrice=[[NSMutableArray alloc]initWithObjects:@"200",@"900",@"6000",@"2220",@"1000",@"",@"5000",@"",@"",nil]; 


dataList=[[NSMutableArray alloc]initWithObjects:@"Praline rose dress",@"Praline rose dress",@"Multi Colored dress",@"Multi Colored dress",@"Multi Colored dress",@"Praline rose dress", 
      @"Multi Colored dress",@"Multi Colored dress",@"Praline rose dress",nil]; 

searchData=[[NSMutableArray alloc]init]; 
[searchData addObjectsFromArray:dataList]; 

dataTable.delegate=self; 
dataTable.dataSource=self; 
[dataTable reloadData]; 

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

    UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 140, 20)]; 
    labelOne.tag = 100; 
    labelOne.backgroundColor=[UIColor clearColor]; 
    labelOne.font=[UIFont systemFontOfSize:14]; 
    [cell.contentView addSubview:labelOne]; 
    [labelOne release]; 


    labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)]; 
    labelTwo.tag = 101; 
    labelTwo.backgroundColor=[UIColor clearColor]; 
    labelTwo.font=[UIFont systemFontOfSize:14]; 
    labelTwo.textColor=[UIColor colorWithRed:0.25098 green:0.447059 blue:0.07451 alpha:1]; 
    [cell.contentView addSubview:labelTwo]; 
    [labelTwo release]; 


    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(70, 30, 140, 20)]; 
    label3.tag = 102; 
    label3.backgroundColor=[UIColor clearColor]; 
    label3.font=[UIFont systemFontOfSize:12]; 
    [cell.contentView addSubview:label3]; 
    [label3 release]; 


    UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(260, 30, 140, 20)]; 
    label4.tag = 103; 
    label4.backgroundColor=[UIColor clearColor]; 
    label4.font=[UIFont systemFontOfSize:12]; 
    [cell.contentView addSubview:label4]; 
    [label4 release]; 
} 

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

labelTwo = (UILabel *) [cell.contentView viewWithTag:101]; 
labelTwo.text = [[NSString alloc]initWithFormat:@"%@",[newPrice objectAtIndex:indexPath.row]]; 

UILabel *label3 = (UILabel *) [cell.contentView viewWithTag:102]; 
label3.text=[[NSString alloc]initWithFormat:@"%@",[details objectAtIndex:indexPath.row]]; 

UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103]; 
label4.text=[[NSString alloc]initWithFormat:@"%@",[oldPrice objectAtIndex:indexPath.row]]; 



return cell; 

}

回答

1
if(btn.tag == 500) 
{ 
    aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"OrderDate" ascending:YES]; 
} 
if(btn.tag == 501) 
{ 
    aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"CustomerGuid" ascending:YES];  
} 
if (searching) { 
    [temp_details sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]]; 
} 
else{ 
    [orders sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]]; 
} 
[self.tableView reloadData]; 

設置的標籤有兩個按鈕,你可以按照上面的方法進行排序 這些密鑰在從設置到標籤的表視圖字典我正在整理那些使用按鈕按下的標籤

+0

@克里希納..什麼是initwithkey意味着排序字符串:我已閱讀文檔,但不明白..請你詳細說明一下。謝謝 – NoviceDeveloper 2012-02-15 06:01:20

+0

那些字典裏面的關鍵字,我將它設置到tableview標籤中,這樣排序字典,我重新載入表格 – 2012-02-15 06:13:08

+0

所以我必須在我的應用中使用字典嗎?我還沒有使用任何字典。 – NoviceDeveloper 2012-02-15 06:18:48

0

你可以看到關於以下方法的NSArray類的引用。

排序

sortedArrayHint 
sortedArrayUsingFunction:context: 
sortedArrayUsingFunction:context:hint: 
sortedArrayUsingDescriptors: 
sortedArrayUsingSelector: 
sortedArrayUsingComparator: 
sortedArrayWithOptions:usingComparator: 

至於你的問題,你可以通過

[strings sortedArrayUsingSelector:@selector(compare:)]