2012-02-15 86 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:)]