2012-01-31 22 views
2

首先,我得到了一些產品來自IAP如何根據價格對產品進行分類在App Purchase中?

-(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; 
} 

回答

0

你想要做的是在嘗試讀取它之前對NSArray進行排序。排序NSArray有很多選項,其中大多數都涉及創建自己的排序方法。你做類似的事情:

[productDetailList sortedArrayUsingFunction:intSort context:NULL]; 

這將使用您指定的比較器方法。比較方法用於一次比較兩個元素,如果第一個元素小於第二個元素,則返回NSOrderedAscending,如果第一個元素大於第二個元素,則應該返回NSOrderedDescending;如果元素相等,則返回NSOrderedSame。每次調用比較函數時,都會傳遞上下文作爲第三個參數。這使得比較可以基於一些外部參數,比如字符排序是區分大小寫還是不區分大小寫,但在這種情況下這並不重要。你必須要實現的功能是這樣的:

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

對於上述方法的詳細信息,看看在documentation,它給你一個例子。

所以你可以像這樣對數組進行排序,或者每次添加內容時都可以選擇排序數組。所以每次添加一個對象時都要進行排序,並確保將它放在正確的位置以保持數組始終排序。

取決於你想要什麼,我會說保持陣列不斷排序插入時間是最好的選擇,所以你不必浪費時間通過排序數組構建視圖。要做到這一點很簡單,每次你在數組中輸入一些東西時,你會遍歷數組,直到你找到一個價格大於你想要輸入的對象,然後你在該位置插入對象條目。

+0

您好,我有一個錯誤的原因:「 - [SKProduct比較:]:無法識別的選擇發送到實例0x2e2ee0」 – jean 2012-01-31 07:30:36

+0

裏面你SKProduct沒有你實現可以做到以上的比較函數? '比較(id arg1,id arg2,void * context)'?如果arg1 arg2返回NSOrderedDescending,並且如果arg1 == arg2返回NSOrderedSame。一旦你在SKProduct中實現了這個功能,你可以調用'[productDetailList sortedArrayUsingFunction:compare context:NULL];'並且應該可以工作,如果不行請發表更多的結果和/或你的代碼。 – 2012-01-31 08:02:43

14
NSArray *products = [response.products sortedArrayUsingComparator:^(id a, id b) { 
     NSDecimalNumber *first = [(SKProduct*)a price]; 
     NSDecimalNumber *second = [(SKProduct*)b price]; 
     return [first compare:second]; 
    }]; 
+0

偉大的解決方案,正是OP所需要的。 – pabloruiz55 2012-05-10 08:56:31

+0

這絕對應該是被接受的答案 – orenk86 2014-08-25 17:14:55

0

在迅速

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)} 
2

斯威夫特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)") 
    } ... 
0

斯威夫特3,無需self變量。從低工作對我來說就像一個魅力排序按價格高:

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