我的应用包含两个数组(即productArray和priceArray),并在单元格上传递一个数组的值。 text.eltaillabel.text上的textlabel.text和其他数组。工作正常。现在我想在升序价格表中对uitableview进行排序。请指导我怎么做..这是我的代码..
#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
答案 0 :(得分:5)
从两个不同的数组中读取值以填充tableview单元格会使排序变得困难。我建议您创建一个包含product和price属性的对象,然后将这些对象存储在一个数组中。然后,您可以使用比较价格的自定义排序方法对数组进行排序。
产品类示例:
@interface Product : NSObject
{
NSString *productName;
NSString *price;
}
@property (nonatomic, retain) NSString *productName;
@property (nonatomic, retain) NSString *price;
然后编写一个可以按价格对产品进行排序的方法:
products = [products sortedArrayUsingSelector(sortByPrice:)];
...在您的产品实现中调用此方法:
- (NSComparisonResult)sortByPrice:(id)anObject
{
if (self.price < anObject.price) {
return NSOrderedAscending;
} else (if self.price > anObject.price) {
return NSOrderedDescending;
}
return NSOrderedSame;
}
将产品存储在一个阵列中:
NSMutableArray *products;
您的单元格值将由单个排序数组中的对象设置。
创建产品更改:
[products addObject:@"Milk"];
[prices addObject:@"2.25"];
到此:
Product *newProduct = [[[Product alloc] init] autorelease];
[products addObject:newProduct];
[newProduct setProductName:@"Milk"];
[newProduct setPrice:@"2.25"];
设置单元格值会发生变化:
cell.textLabel.text=[productArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[priceArray objectAtIndex:indexPath.row];
到此:
cell.textLabel.text=(Product *)[products objectAtIndex:indexPath.row].productName;
cell.detailTextLabel.text=(Product *)[products objectAtIndex:indexPath.row].price;