Reputation: 223
I have a UITableView
which i populate using data from an XML file which i read using NSXMLParser
. My UITableview
scrolling is very slow and I only have about 10 sections and 15 rows (about 2 in each section). My code in the cellForRowAtIndexPath
is as follows:
- (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;}
I need your help to determine what is the cause of the slow scrolling as i do not have any images in my cells which cause delays. Is it the sorting i am doing?
Please help...thank you for our time.
Upvotes: 0
Views: 408
Reputation: 223
It was the sorting indeed...I managed to solve it by sorting the array only once when the parsing ends and just using that sorted array thereafter
Upvotes: 1