单击按钮时根据字母或数字顺序对 UITableView 进行排序

发布于 2025-01-06 06:21:08 字数 3266 浏览 1 评论 0原文

我定制了带有四个标签的 uitableview 单元格以及四个保存每个标签数据的数组。标签1包含对象名称,标签2包含价格,标签3和标签4包含其他数据。现在有两个按钮,在第一个按钮上单击我想按对象名称的字母顺序对表进行排序,在第二个按钮上单击我想根据价格排序..请指导我如何做..这是我的代码。 .

  - (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 *CellIdentifier=@"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;

I have customized uitableview cell with four labels and also four arrays which hold data for each labels. label1 contain name of object and label 2 contain price label 3 and label 4 contain other data. Now there is two buttons , on first button click i want to sort the table in alphabetical order of object name and on second button click i want to sort according to price ..Please guide me how to do it.. Here is my code..

  - (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 *CellIdentifier=@"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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慈悲佛祖 2025-01-13 06:21:08
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];

为两个按钮设置标签,即可按照上面的方法进行排序
这些键是来自字典,设置为表视图中的标签,我正在使用按下的按钮对这些标签进行排序

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

set the tags for two buttons and you can follow the above method to sort
those keys are the from dictionary which set to the label in the table view i am sorting those labels using the button pressed

蓝眼睛不忧郁 2025-01-13 06:21:08

您可以查看有关以下方法的 NSArray 类参考。

排序

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

对于您的问题,您可以按以下方式对字符串进行排序

[strings sortedArrayUsingSelector:@selector(compare:)]

You can see NSArray class reference about following methods.

Sorting

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

As for your problem, you can sort strings by

[strings sortedArrayUsingSelector:@selector(compare:)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文