xcode 动态 NumberOfRowsInSection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9240118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic NumberOfRowsInSection
提问by Bruno
I have an iphone App that reads the "category" field from the xml file that is a rss feed. The way my App works is that it displays the content of the rss feed in a table view by categories from the xml "category" field.
我有一个 iphone 应用程序,它从作为 rss 提要的 xml 文件中读取“类别”字段。我的应用程序的工作方式是,它按来自 xml“类别”字段的类别在表格视图中显示 rss 提要的内容。
Im a bit new to tableviews so im a bit lost.
我对 tableviews 有点陌生,所以我有点迷茫。
I just have 2 categories on the xml file, one called "Uncategorized" and another called "Promos".
我在 xml 文件上只有 2 个类别,一个称为“未分类”,另一个称为“促销”。
The current code is:
目前的代码是:
- (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;
}
The problem i have is that it displays the same number of cells for both categories and doesn't filter them by Categories.
我遇到的问题是它为两个类别显示相同数量的单元格,并且没有按类别过滤它们。
Best Regards.
此致。
回答by Matthias Bauch
of course it does. Look at your code (and the comments I added):
当然可以。查看您的代码(以及我添加的注释):
- (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
}
}
put Uncategorized and Promos into different NSArrays.
将 Uncategorized 和 Promos 放入不同的 NSArray 中。
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;
}
回答by NeverBe
- (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];
}
For cell generation you should use this way too. Or you can prefilter data (for speed up)
对于细胞生成,您也应该使用这种方式。或者你可以预过滤数据(为了加速)
回答by oradyvan
that's because you return exactly the same number of items for both 0 and 1 sections: return itemsToDisplay.count
那是因为您为 0 和 1 部分返回了完全相同数量的项目: return itemsToDisplay.count
Moreover, you also use the same data item for both section 0 and 1 cells: MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
此外,您还对第 0 部分和第 1 部分单元格使用相同的数据项: MWFeedItem *item = [itemsToDisplay objectAtIndex:indexPath.row];
You should either have 2 separate arrays of items, for example, itemsUncategorized
and itemsPromos
and make sure they store different data items for your "Uncategorized" and "Promos" lists.
例如,您应该有 2 个单独的项目数组,itemsUncategorized
并itemsPromos
确保它们为您的“未分类”和“促销”列表存储不同的数据项。
Or, you can implement a flag iniside your MWFeedItem
specifying wheteher it is an Uncategorized item or Promos item. This is a bit trickier, but alos possible approach.
或者,您可以在您MWFeedItem
指定的内容中实现一个标志,无论它是未分类的项目还是促销项目。这有点棘手,但也是可能的方法。
Example:
例子:
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