嗨,我制作了一个RSS feed应用程序,该应用程序从多个网站收集了feed项目并将它们发布在UITableView上,我尝试按日期升序对结果进行排序,目前唯一要排序的是数据,其余的则是我的数据(例如“名称”和“说明”)被混淆成随机的顺序,并且与日期不符。
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSSortDescriptor *dateDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"pubDate"
ascending:YES];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss Z"];
NSString *date = [[feeds objectAtIndex:indexPath.row] objectForKey:@"pubDate"];
NSDate *finalDate = [dateFormatter dateFromString:date];
NSLog(@"displayDate = %@", finalDate);
NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];
NSArray *sortedEventArray = [feeds
sortedArrayUsingDescriptors:sortDescriptors];
cell.customName.text = [[sortedEventArray objectAtIndex:indexPath.row] objectForKey: @"author"];
cell.customDescription.text = [[sortedEventArray objectAtIndex:indexPath.row] objectForKey: @"description"];
cell.customDate.text = [NSString stringWithFormat:@"%@", finalDate];
(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc] init];
author = [[NSMutableString alloc] init];
description = [[NSMutableString alloc] init];
pubDate = [[NSMutableString alloc] init];
link = [[NSMutableString alloc] init];
}
}
(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"author"]) {
[author appendString:string];
} else if ([element isEqualToString:@"link"])
{
[link appendString:string];
} else if ([element isEqualToString:@"description"])
{
[description appendString:string];
}
else if ([element isEqualToString:@"pubDate"])
{
[pubDate appendString:string];
}
}
(void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"item"]) {
link = [NSMutableString stringWithString:
[link stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
pubDate = [NSMutableString stringWithString:[pubDate stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
[item setObject:author forKey:@"author"];
[item setObject:link forKey:@"link"];
[item setObject:description forKey:@"description"];
[item setObject:pubDate forKey:@"pubDate"];
[feeds addObject:[item copy]];
}
}
不应在所有cellForRowAtIndexPath上进行排序。当表滚动时,将反复调用该方法。相反,请在完成时将xml项目插入正确排序的位置。
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// your existing code
NSComparator comparator = ^(NSDictionary *a, NSDictionary *b) {
return [a[@"pubDate"] compare:b[@"pubDate"]];
};
NSUinteger index = [feeds indexOfObject:item
inSortedRange:NSMakeRange(0, [feeds count])
options:NSBinarySearchingInsertionIndex
usingComparator:comparator];
[feeds insertObject:[item copy] atIndex:index];
}
不要feeds
在cellForRowAtIndexPath中进行任何排序或操作。刚拿到物品。。。
NSDictionary *myModel = feeds[indexPath.row];
// and use it
cell.customName.text = myModel[@"author"];
更少的代码,更少的计算。
编辑
您的日期解释代码很棒,但是放错了位置。通过在cellForRowAtIndexPath
:中完成它,然后将其丢弃,然后重做,依此类推。而且,它做得太晚了,无法在需要的地方进行排序。
取而代之的是,NSDate
在解析过程中获取一个构建,并保留它。它是排序所需的,对于其他日期计算,您可能需要在应用程序中的其他位置进行。
// first, abstract your (perfectly good) date parse to it's own function
- (NSDate *)dateFromXml:(NSString *)dateString {
// with static declaration, we'll create the date formatter only once
// better than creating a formatter for every date we parse
static NSDateFormatter *df;
if (!df) {
df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"EEEE, dd MMM yyyy HH:mm:ss Z"];
}
return [df dateFromString: dateString];
}
现在,让我们将NSDate表示形式添加到已解析的xml中。从现在开始,pubDate仅用于显示,我们将添加另一个名为pubDateObj的键来执行任何日期计算,例如排序:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// your existing code
// now add the new pubDateObj to the parse
item[@"pubDateObj"] = [self dateFromXml:item[@"pubDate"]];
// the sort code I suggest above, except:
NSComparator comparator = ^(NSDictionary *a, NSDictionary *b) {
return [a[@"pubDateObj"] compare:b[@"pubDateObj"]];
};
// and so on as before
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句