My app contain two array (ie productArray and priceArray) and pass the value of one array on cell. textlabel.text and other array on cell.detaillabel.text.It is working fine .Now i want to sort uitableview in ascending price list. please guide me how to do it.. Here Is my code..
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSMutableArray *productArray;
NSMutableArray *priceArray;
}
@property(nonatomic,retain)NSMutableArray *productArray;
@property(nonatomic,retain)NSMutableArray *priceArray;
@end
#import "RootViewController.h"
@implementation RootViewController
@synthesize productArray,priceArray;
- (void)viewDidLoad {
[super viewDidLoad];
productArray=[[NSMutableArray alloc]initWithObjects:@"Apple",@"iphone",@"ipod",@"ipad",nil];
priceArray=[[NSMutableArray alloc]initWithObjects:@"4000",@"2000",@"100",@"1000",nil];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [productsArray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.textLabel.text=[productsArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[priceArray objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
Reading values from two different arrays to populate a tableview cell makes sorting difficult. I recommend you make a single object that contains both the product and price attributes, then store these objects in a single array. Then you can simply sort the array with a custom sorting method that compares the prices.
Example product class:
Then write a method that can sort products by price:
…which calls this method in your products implementation:
Store products in one array:
And your cell values will be set by an object from a single, sorted array.
Creating a product changes from this:
to this:
And setting your cell values changes from this:
to this: