我有一个UITableView
,我使用NSXMLParser
读取的XML文件中的数据进行填充。我的UITableview
滚动非常慢,我只有大约10个部分和15行(每个部分大约2个)。我在cellForRowAtIndexPath
中的代码如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Declare the cell identifier for reuse
static NSString *SectionsTableIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier];
//Check if the table view cell has been created
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SectionsTableIdentifier] autorelease];
}
CSCustomCellBackgroundView *cellBackgound = [[CSCustomCellBackgroundView alloc] init];
cellBackgound.position = CustomCellBackgroundViewPositionPlain;
cell.selectedBackgroundView = cellBackgound;
[cellBackgound release];
//Sort the years descending
allKeys = [[listofnews allKeys] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]autorelease]];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *obj1Date;
NSDate *obj2Date;
obj1Date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@%@",@"01/",obj1]];
obj2Date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@%@",@"01/",obj2]];
NSComparisonResult result = [obj2Date compare:obj1Date];
return result;
}];
aKey = [allKeys objectAtIndex:indexPath.section];
NSArray *sortedRowsbyDate = [[listofnews objectForKey:aKey] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]autorelease]];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSDate *obj1Date;
NSDate *obj2Date;
obj1Date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@%@",@"01/",obj1]];
obj2Date = [dateFormatter dateFromString:[NSString stringWithFormat:@"%@%@",@"01/",obj2]];
NSComparisonResult result = [obj2Date compare:obj1Date];
return result;
}];
cell.textLabel.text = [[sortedRowsbyDate objectAtIndex:indexPath.row] objectForKey:@"sTitle"];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.text = [[sortedRowsbyDate objectAtIndex:indexPath.row] objectForKey:@"sDescription"];
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = UILineBreakModeTailTruncation;
cell.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
cell.detailTextLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.separatorColor = [UIColor colorWithRed: (109.0/255) green: (159.0/255) blue: (0.0/255) alpha: 1.0f];
return cell;}
我需要你的帮助来确定缓慢滚动的原因是什么,因为我的单元格中没有任何导致延迟的图像。这是我正在做的排序吗?
请帮助......谢谢你们的时间。
答案 0 :(得分:1)
这确实是排序......我设法通过在解析结束时仅将数组排序一次并且之后只使用该排序数组来解决它