2012-02-16 42 views
1

我的应用程序包含两个数组(即productArray和priceArray)并传递单元格上的一个数组的值。 textlabel.text和其他数组cell.detaillabel.text.It工作正常。现在我想按照升序价格列表UICEVIEW排序。请指导我如何做到这一点..这是我的代码..按照升序价格排序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

你应该把原始的源代码放在这里,不要包含我的建议中的代码,因为这个问题会让所有看到它的人感到困惑。 – bneely 2012-02-16 12:21:04

+2

@bneely.Updated :) – NoviceDeveloper 2012-02-16 12:34:42

回答

4

从两个不同的数组中读取值填充tableview单元格使排序困难。我建议您制作一个包含产品和价格属性的单个对象,然后将这些对象存储在一个数组中。然后,您可以简单地使用比较价格的自定义排序方法对数组进行排序。

例产品类别:

@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; 
+0

我应该使用NSDictionary或其他东西..我抓我的头..请引导我或引用我一个教程。谢谢 – NoviceDeveloper 2012-02-16 11:21:12

+0

我更新了我的答案与示例代码。这有帮助吗? – bneely 2012-02-16 11:22:53

+0

一件事更多..如何将值传递给cell.textlabel和cell.detaillabel ..我这样做,但它现在显示anything.cell.textLabel.text = [products objectAtIndex:indexPath.row]; \t cell.detailTextLabel.text = [products objectAtIndex:indexPath.row]; – NoviceDeveloper 2012-02-16 11:37:04