Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么我的tableview只按升序显示日期,而不显示其余数据_Ios_Iphone_Objective C_Uitableview - Fatal编程技术网

Ios 为什么我的tableview只按升序显示日期,而不显示其余数据

Ios 为什么我的tableview只按升序显示日期,而不显示其余数据,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,嗨,我制作了一个RSS提要应用程序,它从多个网站收集提要项目,并将它们发布到UITableView上,我曾尝试按日期升序排列结果,目前唯一排序的是数据和我的其他数据,如“姓名”和“描述”,并被混淆成随机顺序,不与日期对齐 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell *cell = [tableView deque

嗨,我制作了一个RSS提要应用程序,它从多个网站收集提要项目,并将它们发布到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]];

}

}

您可以从未排序的
feeds
数组中设置
date
变量,但可以从排序的
sortedventarray
数组中获取其他数据

NSString *date = [[feeds objectAtIndex:indexPath.row] objectForKey:@"pubDate"];
NSDate *finalDate = [dateFormatter dateFromString:date];
然后,在构建排序数组之后:

cell.customName.text = [[sortedEventArray objectAtIndex:indexPath.row] objectForKey: @"author"];
cell.customDescription.text = [[sortedEventArray objectAtIndex:indexPath.row] objectForKey: @"description"];

cell.customDate.text = [NSString stringWithFormat:@"%@", finalDate];
在构建排序数组后,应将两行移动到设置最终日期的位置,如下所示:

NSArray *sortedEventArray = [feeds
                         sortedArrayUsingDescriptors:sortDescriptors];
// Put the two lines here and access the sorted array:
NSString *date = [[sortedEventArray objectAtIndex:indexPath.row] objectForKey:@"pubDate"];
NSDate *finalDate = [dateFormatter dateFromString:date];
编辑:

您可能应该提取数组元素一次,然后根据需要使用它:

NSDictionary *cellItem = [sortedEventArray objectAtIndex:indexPath.row];
cell.customName.text = [cellItem objectForKey: @"author"];
cell.customDescription.text = [cellItem objectForKey: @"description"];
// and so on...

不应在所有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];
}
不要在cellForRowAtIndexPath中对源进行任何排序或操作。就拿这个东西

NSDictionary *myModel = feeds[indexPath.row];
// and use it
cell.customName.text = myModel[@"author"];
代码更少,计算也更少

编辑

你的日期解释代码很棒,但放错地方了。通过在
cellforrowatinexpath
中执行:完成后,抛出,然后重做,依此类推。而且,在我们需要它的地方,它做得太晚,在排序中没有用处

相反,在解析过程中生成一个
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
}

我建议不要对CellForRowatineXpath中的数组进行排序(并重新排序)。我同意,排序应该移出该函数。。。排序问题一解决,就应该立即进行排序-另一方面,如果主要颠簸得到平滑,事情可能会变得更容易…啊,是的-很多颠簸都得到了很好的平滑:-)运行得更平滑,但由于某种原因,数据仍然处于混乱的顺序,并且没有从日期开始升序。有什么想法吗?在插入之后,NSLog(@“%@”,feed)怎么样;看看它是否按你的期望排序。如果是的话,那么在cellForRowAtIndexPath中访问它的方式就有一个错误:是的,我发现它是按天排序的,而且也是按随机天数排序的。从星期五开始,然后到星期一,然后到星期六等等。。。奇怪。谢谢你迄今为止的帮助,但这可能与日期的格式有关吗?我已经记录了提要,它产生了与TableView相同的排序。这是词汇顺序。这意味着pubDate不是NSDate。它必须是一个以日期开始的字符串。请在此处粘贴发布日期。我们会把它整理好的。