单击按钮时根据字母或数字顺序对 UITableView 进行排序
我定制了带有四个标签的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为两个按钮设置标签,即可按照上面的方法进行排序
这些键是来自字典,设置为表视图中的标签,我正在使用按下的按钮对这些标签进行排序
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
您可以查看有关以下方法的 NSArray 类参考。
排序
对于您的问题,您可以按以下方式对字符串进行排序
You can see NSArray class reference about following methods.
Sorting
As for your problem, you can sort strings by