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;
}
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
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:)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.