如何按价格对应用内购买的产品进行排序?
首先,我从IAP获取了一些产品,
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
[productDetailsList addObjectsFromArray: response.products];
[productDisplayTableView reloadData];
}
如何将它们按产品价格排序放在uitableview中?谢谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *GenericTableIdentifier = @"GenericTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: GenericTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:GenericTableIdentifier] autorelease];
}
SKProduct *thisProduct = [productDetailsList objectAtIndex:row];
NSUInteger row = [indexPath row];
[button setTitle:localizedMoneyString forState:UIControlStateNormal];
[cell.contentView addSubview:button];
return cell;
}
First,I got some products from IAP
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
[productDetailsList addObjectsFromArray: response.products];
[productDisplayTableView reloadData];
}
How to put them in a uitableview sort by product price? thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *GenericTableIdentifier = @"GenericTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: GenericTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:GenericTableIdentifier] autorelease];
}
SKProduct *thisProduct = [productDetailsList objectAtIndex:row];
NSUInteger row = [indexPath row];
[button setTitle:localizedMoneyString forState:UIControlStateNormal];
[cell.contentView addSubview:button];
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您想要做的是在尝试读取 NSArray 之前对它进行排序。对 NSArray 进行排序有很多选项,其中大多数都涉及创建您自己的排序方法。您执行以下操作:
这将使用您指定的比较器方法。比较方法用于一次比较两个元素,如果第一个元素小于第二个元素,则返回 NSOrderedAscending;如果第一个元素大于第二个元素,则返回 NSOrderedDescending;如果元素相等,则返回 NSOrderedSame。每次调用比较函数时,都会将上下文作为其第三个参数传递。这允许比较基于一些外部参数,例如字符排序是区分大小写还是不区分大小写,但这对于您的情况并不重要。您必须实现的功能如下所示:
有关上述方法的更多信息,请查看 文档,它给了你一个例子。
因此,您可以像这样对数组进行排序,或者每次添加内容时您始终可以选择对数组进行排序。因此,每次添加对象时,您都会自己进行排序,并确保将其放在正确的位置以保持数组始终排序。
根据您想要的,我想说保持数组在插入时间不断排序是最好的选择,这样您就不必在通过对数组排序来构建视图时浪费时间。这样做很简单,每次在数组中输入一些内容时,都会迭代数组,直到找到一个价格大于您要输入的价格的对象,然后将该对象插入到该对象之前的位置入口。
What you want to do is sort the NSArray prior to trying to read it. There are many options for sorting NSArray, most of them involve creating your own sorting method. You do something like:
That will use your comparator method that you specify. The comparison method is used to compare two elements at a time and should return NSOrderedAscending if the first element is smaller than the second, NSOrderedDescending if the first element is larger than the second, and NSOrderedSame if the elements are equal. Each time the comparison function is called, it’s passed context as its third argument. This allows the comparison to be based on some outside parameter, such as whether character sorting is case-sensitive or case-insensitive, but this doesn't matter in your case. The function you have to implement would look like this:
For more information of the above method take a look at the documentation, it give you an example.
So you can sort the array like this or you always have the choice of sorting the array every time you add something. So every time you add an object you do the sorting yourself and make sure to put it in the right location to keep the array always sorted.
Depending on what you want, I would say keeping the array constantly sorted on insertion time is the best option, so you don't have to waste time while building the view by sorting the array. To do it like this would be simple, every time you enter something into the array, you iterate through the array until you find an object with a price larger than the one you want to enter, then you insert the object at the location before that entry.
迅速
In swift
斯威夫特2.1.1
Swift 2.1.1
Swift 3,不需要
self
变量。对我来说就像一个按价格从低到高排序的魅力:编辑:针对 Swift 5 进行了更新。
Swift 3, without the need for
self
variables. Worked for me like a charm to sort by price from low to high:EDIT: Updated for Swift 5.