2012-02-11 54 views
0

我有一個iPhone應用程序,從XML文件是rss訂閱源讀取「類別」字段。我的應用程序的工作方式是它通過xml「category」字段中的類別在表格視圖中顯示rss feed的內容。動態NumberOfRowsInSection

我是個有點新tableviews所以我是一個有點失落。

我只是有2個類別的XML文件,一個名爲「未分類」,另一個叫「促銷」。

當前的代碼是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 
// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    switch (section) { 
     case 0:   
      return itemsToDisplay.count; 
     default: 
      return itemsToDisplay.count; 
    } 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 

    if(section == 0) 
     return @"Promoções"; 
    else 
     return @"Não Categorizados"; 
} 
// 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]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 
    if (indexPath.section == 0) { 
     MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row]; 
     if([item.category isEqualToString:@"PROMOS"]){ 
      MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row]; 
      NSLog(@"ENTRA NO PROMOS____________________"); 
      NSLog(@"item.category = %@-------------->", item.category); 
      // Process 
      NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]"; 
      NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : @"[No Summary]"; 
      NSLog(@"IMAGE (table View) = %@",item.image); 

      NSURL *url = [NSURL URLWithString:item.image]; 
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
      [request setDelegate:self]; 
      [request startAsynchronous]; 
      NSData * imageData = [NSData dataWithContentsOfURL:url]; 
      UIImage * image = [UIImage imageWithData:imageData]; 
      // Set 
      cell.imageView.image = image; 
      cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 
      cell.textLabel.text = itemTitle; 
      NSMutableString *subtitle = [NSMutableString string]; 
      if (item.date) [subtitle appendFormat:@"%@: ", [formatter stringFromDate:item.date]]; 
      [subtitle appendString:itemSummary]; 
      cell.detailTextLabel.text = subtitle; 
      NSLog(@"FIM DO PROMOS_____________________"); 
     } 
    }else if(indexPath.section == 1){ 
     MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row]; 
     if([item.category isEqualToString:@"Uncategorized"]){ 
      NSLog(@"ENTRA NO UNCATEGORIZED__________"); 
      NSLog(@"item.category = %@------------------>", item.category); 
      // Process 
      NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]"; 
      NSString *itemSummary = item.summary ? [item.summary stringByConvertingHTMLToPlainText] : @"[No Summary]"; 
      NSLog(@"IMAGE (table View) = %@",item.image); 

      NSURL *url = [NSURL URLWithString:item.image]; 
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
      [request setDelegate:self]; 
      [request startAsynchronous]; 
      NSData * imageData = [NSData dataWithContentsOfURL:url]; 
      UIImage * image = [UIImage imageWithData:imageData]; 
      // Set 
      cell.imageView.image = image; 
      cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 
      cell.textLabel.text = itemTitle; 
      NSMutableString *subtitle = [NSMutableString string]; 
      if (item.date) [subtitle appendFormat:@"%@: ", [formatter stringFromDate:item.date]]; 
      [subtitle appendString:itemSummary]; 
      cell.detailTextLabel.text = subtitle; 
      NSLog(@"FIM DO UNCATEGORIZED________________"); 
     } 
    } 
    return cell; 
} 

我的問題是,它顯示了相同數量的細胞兩個類別的,並且不通過關鍵字進行過濾。

最好的問候。

回答

1
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category = %@", @"PROMOS"]; 

NSArray *promosArray = [itemsToDisplay filteredArrayUsingPredicate:predicate]; 

switch (section) { 

    case 0: 

     return [promosArray count]; 
    default: 

     return [itemsToDisplay count] - [promosArray count]; 
} 

對細胞的產生,你應該也用這種方式。或者,你可以預過濾數據(加速)

2

當然是的。看看你的代碼(我加了註釋):

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    switch (section) { 
     case 0:   
      return itemsToDisplay.count; // <- this 
     default: 
      return itemsToDisplay.count; // is exactly the same line of code as this one 
    } 
} 

把未分類和促銷成不同NSArrays。

NSArray *promos;    // add all promos to this array 
NSArray *uncategorized;  // and eerything else into this array 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    switch (section) { 
     case 0:   
      return [promos count];   // return the number of promos 
     default: 
      return [uncategorized count]; // return the number of uncategorized objects 
    } 
    return 0; 
} 
1

那是因爲你只返回了0和1段相同數量的項目:return itemsToDisplay.count

此外,您還可以使用相同數據項都節0 1個細胞:MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];

您應該有2個獨立的項目陣列,例如,itemsUncategorizeditemsPromos,並確保它們存儲你的「未分類」和「促銷」列出了不同的數據項。

或者,您可以在您的MWFeedItem上實施標誌,指定它是未分類商品還是促銷商品。這有點棘手,但可能的方法。

例子:

typedef enum { 
    ITEM_UNCATEGORIZED, 
    ITEM_PROMOS, 
} ITEM_TYPE; 

@interface MWFeedItem { 
@private 
    NSString * title; 
    NSString * summary; 
    UIImage * image; 
    ITEM_TYPE itemType; 
} 

// TODO: put your properties here for the ivars of this class... 
// TODO: put your item methods here, if any... 
@end 
+0

他已經使用了 「標誌」。他的category屬性表示如果一個對象是一個促銷或者它是未分類的('[item.category isEqualToString:@「PROMOS」]')。這面旗子根本無助於他。 – 2012-02-11 12:45:18

+0

你是對的,除了在項目中使用標誌還需要篩選出這些未分類和促銷項目。舉例來說,在下面使用謂詞作爲@NeverBe。 – oradyvan 2012-02-11 12:52:16

相關問題