如何按价格对应用内购买的产品进行排序?

发布于 2024-12-29 16:35:13 字数 1078 浏览 2 评论 0原文

首先,我从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

沩ん囻菔务 2025-01-05 16:35:14

您想要做的是在尝试读取 NSArray 之前对它进行排序。对 NSArray 进行排序有很多选项,其中大多数都涉及创建您自己的排序方法。您执行以下操作:

[productDetailList sortedArrayUsingFunction:intSort context:NULL];

这将使用您指定的比较器方法。比较方法用于一次比较两个元素,如果第一个元素小于第二个元素,则返回 NSOrderedAscending;如果第一个元素大于第二个元素,则返回 NSOrderedDescending;如果元素相等,则返回 NSOrderedSame。每次调用比较函数时,都会将上下文作为其第三个参数传递。这允许比较基于一些外部参数,例如字符排序是区分大小写还是不区分大小写,但这对于您的情况并不重要。您必须实现的功能如下所示:

NSInteger intSort(id num1, id num2, void *context)

有关上述方法的更多信息,请查看 文档,它给了你一个例子。

因此,您可以像这样对数组进行排序,或者每次添加内容时您始终可以选择对数组进行排序。因此,每次添加对象时,您都会自己进行排序,并确保将其放在正确的位置以保持数组始终排序。

根据您想要的,我想说保持数组在插入时间不断排序是最好的选择,这样您就不必在通过对数组排序来构建视图时浪费时间。这样做很简单,每次在数组中输入一些内容时,都会迭代数组,直到找到一个价格大于您要输入的价格的对象,然后将该对象插入到该对象之前的位置入口。

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:

[productDetailList sortedArrayUsingFunction:intSort context:NULL];

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:

NSInteger intSort(id num1, id num2, void *context)

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.

回忆凄美了谁 2025-01-05 16:35:14

迅速

 var validProducts = response.products
        for var i = 0; i < validProducts.count; i++
        {
            self.product = validProducts[i] as? SKProduct
            self.productsArray.append(product!)
            println(product!.localizedTitle)
            println(product!.localizedDescription)
            println(product!.price)


        }
        self.productsArray.sort{($0.price < $1.price)}

In swift

 var validProducts = response.products
        for var i = 0; i < validProducts.count; i++
        {
            self.product = validProducts[i] as? SKProduct
            self.productsArray.append(product!)
            println(product!.localizedTitle)
            println(product!.localizedDescription)
            println(product!.price)


        }
        self.productsArray.sort{($0.price < $1.price)}
清眉祭 2025-01-05 16:35:14
self.products = products!.sorted(by: { (item1, item2) -> Bool in
            return item1.price.doubleValue < item2.price.doubleValue
        })
self.products = products!.sorted(by: { (item1, item2) -> Bool in
            return item1.price.doubleValue < item2.price.doubleValue
        })
原来是傀儡 2025-01-05 16:35:13
NSArray *products = [response.products sortedArrayUsingComparator:^(id a, id b) {
        NSDecimalNumber *first = [(SKProduct*)a price];
        NSDecimalNumber *second = [(SKProduct*)b price];
        return [first compare:second];
    }];
NSArray *products = [response.products sortedArrayUsingComparator:^(id a, id b) {
        NSDecimalNumber *first = [(SKProduct*)a price];
        NSDecimalNumber *second = [(SKProduct*)b price];
        return [first compare:second];
    }];
披肩女神 2025-01-05 16:35:13

斯威夫特2.1.1

public func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    let unsortedProducts = response.products
    let products = unsortedProducts.sort{($0.price.compare($1.price) == NSComparisonResult.OrderedAscending)}
    for p in products {
        print("Found product: \(p.productIdentifier) \(p.localizedTitle) \(p.price.floatValue)")
    } ...

Swift 2.1.1

public func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
    let unsortedProducts = response.products
    let products = unsortedProducts.sort{($0.price.compare($1.price) == NSComparisonResult.OrderedAscending)}
    for p in products {
        print("Found product: \(p.productIdentifier) \(p.localizedTitle) \(p.price.floatValue)")
    } ...
哑剧 2025-01-05 16:35:13

Swift 3,不需要 self 变量。对我来说就像一个按价格从低到高排序的魅力:

let validProducts = response.products
var productsArray = [SKProduct]()
for i in 0 ..< validProducts.count {
    let product = validProducts[i]
    productsArray.append(product)
}
productsArray.sort{(Double(truncating: $0?.price) < Double(truncating: $1?.price))}

编辑:针对 Swift 5 进行了更新。

Swift 3, without the need for self variables. Worked for me like a charm to sort by price from low to high:

let validProducts = response.products
var productsArray = [SKProduct]()
for i in 0 ..< validProducts.count {
    let product = validProducts[i]
    productsArray.append(product)
}
productsArray.sort{(Double(truncating: $0?.price) < Double(truncating: $1?.price))}

EDIT: Updated for Swift 5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文