Iphone 升序价目表中的排序uitableview

Iphone 升序价目表中的排序uitableview,iphone,uitableview,Iphone,Uitableview,我的应用程序包含两个数组(即productArray和priceArray),并在单元格上传递一个数组的值。text label.text和cell.detaillabel.text上的其他数组工作正常。现在我想在升序价目表中对uitableview进行排序。请指导我怎么做。。这是我的密码 #import <UIKit/UIKit.h> @interface RootViewController : UITableViewController { NSMutableArray *

我的应用程序包含两个数组(即productArray和priceArray),并在单元格上传递一个数组的值。text label.text和cell.detaillabel.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
#导入
@接口RootViewController:UITableViewController{
NSMutableArray*productArray;
NSMutableArray*priceArray;
}
@属性(非原子,保留)NSMutableArray*productArray;
@属性(非原子,保留)NSMutableArray*priceArray;
@结束
#导入“RootViewController.h”
@RootViewController的实现
@综合产品阵列、价格阵列;
-(无效)viewDidLoad{
[超级视图下载];
productArray=[[NSMutableArray alloc]initWithObjects:@“苹果”、“iphone”、“ipod”、“ipad”、nil];
priceArray=[[NSMutableArray alloc]initWithObjects:@“4000”,“2000”,“100”,“1000”,nil];
//取消对以下行的注释,以在此视图控制器的导航栏中显示编辑按钮。
//self.navigationItem.rightBarButtonItem=self.editButtonItem;
}
#布拉格标记-
#pragma标记表视图数据源
//自定义表视图中的节数。
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
//自定义表视图中的行数。
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[产品射线计数];
}
//自定义表格视图单元格的外观。
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:CellIdentifier]自动释放];
}
//配置单元格。
cell.textLabel.text=[productsArray objectAtIndex:indexath.row];
cell.detailTextLabel.text=[priceArray objectAtIndex:indexath.row];
返回单元;
}
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//放弃所有未使用的缓存数据、图像等的所有权。
}
-(无效)视图卸载{
//放弃可在viewDidLoad或按需重新创建的任何内容的所有权。
//例如:self.myOutlet=nil;
}
-(无效)解除锁定{
[super dealoc];
}
@结束

从两个不同的数组读取值以填充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;
}
您的单元格值将由单个排序数组中的对象设置

创建产品时,将从此更改:

[products addObject:@"Milk"];
[prices addObject:@"2.25"];
cell.textLabel.text=[productArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[priceArray objectAtIndex:indexPath.row];
为此:

Product *newProduct = [[[Product alloc] init] autorelease];
[products addObject:newProduct];
[newProduct setProductName:@"Milk"];
[newProduct setPrice:@"2.25"];
cell.textLabel.text=(Product *)[products objectAtIndex:indexPath.row].productName;
cell.detailTextLabel.text=(Product *)[products objectAtIndex:indexPath.row].price;
设置单元格值时会发生以下变化:

[products addObject:@"Milk"];
[prices addObject:@"2.25"];
cell.textLabel.text=[productArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[priceArray objectAtIndex:indexPath.row];
为此:

Product *newProduct = [[[Product alloc] init] autorelease];
[products addObject:newProduct];
[newProduct setProductName:@"Milk"];
[newProduct setPrice:@"2.25"];
cell.textLabel.text=(Product *)[products objectAtIndex:indexPath.row].productName;
cell.detailTextLabel.text=(Product *)[products objectAtIndex:indexPath.row].price;

我应该用字典还是别的什么。。我在挠头。请给我指导或给我一个教程。谢谢你还有什么。。如何将值传递给cell.textlabel和cell.detaillabel。。我是这样做的,但现在它显示了任何内容。cell.textlab.text=[products objectAtIndex:indexPath.row];cell.detailTextLabel.text=[products-objectAtIndex:indexath.row];如果没有看到你所有的代码,我不确定。但请使用[[products objectAtIndex:indexPath.row]productName]而不是[products objectAtIndex:indexPath.row].productName]。对于productName变量,您需要有一个getter方法,无论您是@synthesis它还是自己实现它。@bneely。。非常感谢……这很有效。你是我真正的救世主。再次感谢。@VivekSehrawat您能解释一下您不理解的部分吗?问一个新问题可能会更好。如果这里有任何不熟悉的代码,您可以先查看Apple的UITableView和UITableViewCell文档,或者阅读一些Objective-C编程教程。您应该将原始源代码放在这里,而不包括我建议中的代码,因为这个问题会让其他看到它的人感到困惑。