gpt4 book ai didi

iphone - 动态 NumberOfRowsInSection

转载 作者:行者123 更新时间:2023-11-28 19:20:34 26 4
gpt4 key购买 nike

我有一个 iphone 应用程序,它从作为 rss 提要的 xml 文件中读取“类别”字段。我的应用程序的工作方式是它按 xml“类别”字段中的类别在表格 View 中显示 rss 提要的内容。

我对表格 View 有点陌生,所以我有点迷茫。

我在 xml 文件中只有 2 个类别,一个称为“未分类”,另一个称为“促销”。

当前代码为:

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

我遇到的问题是它为两个类别显示相同数量的单元格并且不按类别过滤它们。

最好的问候。

最佳答案

当然可以。查看您的代码(以及我添加的注释):

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

将 Uncategorized 和 Promos 放入不同的 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;
}

关于iphone - 动态 NumberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9240118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com