我有一个 UITableView 填充用户查找的搜索结果。为了做到这一点,我使用了一个 NSMutableArray 的字典,其中为前 10 个添加对象,然后当用户滚动到底部时,它会填充下一个 10,直到没有结果可显示。
这一切都很好,但我开始注意到完成的搜索越多,表格越慢。这是一些代码:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self.objectsArray removeAllObjects];
[self.objectsArray setArray:nil];
[itemsTable reloadData];
[itemsTable scrollRectToVisible:CGRectMake(0, 0, 0, 0) animated:false];
[self loadItemsFromURL:searchURL withItemDescription:encodedString atStartRow:start andEndRow:end];
}
以上是执行新搜索时。然后它执行一个 NSURLConnection 并用这个来响应:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if (self.objectsArray == nil)
self.objectsArray = [NSMutableArray array];
// self.objectsArray = [[NSMutableArray alloc] init];
NSError *error;
NSDictionary *returnArray = [[NSJSONSerialization JSONObjectWithData:itemsData options:kNilOptions error:&error] valueForKey:@"items"];
for (id key in returnArray)
{
[self.objectsArray addObject:[returnArray objectForKey:key]];
}
counter += 10;
[itemsTable reloadData];
}
如您所见,如果用户进行新搜索,所有对象都将被删除,[self.objectsArray removeAllObjects]
我什至尝试将数组设置为nil
. 如果我执行多次搜索,UITableView
每次滚动都会变得越来越慢。几乎就像控制器看到数组随着每次搜索而变得越来越大,即使我在搜索之前从中删除了所有对象。有什么想法,或者我是否以错误的方式解决这个问题?
编辑:
这是cellForRowAtIndexPath:
方法。cell
是一个子类UITableViewCell
。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Product Cell";
static NSString *LoadCellIdentifier = @"Loading Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if ([self.objectsArray count] <= 0 )
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.itemName.text = @"No items found.";
cell.itemPrice.text = @"";
cell.itemLocation.text = @"";
cell.addButton.hidden = YES;
}
else
{
if ([indexPath row] == [self.objectsArray count])
{
if ( [self.objectsArray count] >= 10 )
{
if ( [self.objectsArray count] < counter)
{
cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
[cell.loadingSpinner stopAnimating];
cell.itemName.text = @"No more items found.";
}
else
{
if (!running)
{
[self loadItemsFromURL:searchURL withItemDescription:encodedString atStartRow:[self.objectsArray count] + 1 andEndRow:[self.objectsArray count] + 10];
cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
cell.itemName.text = @"Loading more items...";
[cell.loadingSpinner startAnimating];
running = true;
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:LoadCellIdentifier];
[cell.loadingSpinner startAnimating];
}
}
}
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *match = [self.objectsArray objectAtIndex:[indexPath row]];
cell.addButton.hidden = NO;
if ([match valueForKey:@"DESCRIPTION"] == [NSNull null] )
{
cell.itemName.text = @"Description not available.";
}
else
{
cell.itemName.text = [match valueForKey:@"DESCRIPTION"];
}
if ([match valueForKey:@"AD"] != [NSNull null])
{
NSMutableString *adString = [NSMutableString stringWithString:[match valueForKey:@"AD"]];
NSRange textRange;
textRange = [adString rangeOfString:@"1/"];
if (textRange.location != NSNotFound)
{
[adString replaceCharactersInRange:[adString rangeOfString:@"1/"] withString:@"$"];
}
else
{
[adString replaceCharactersInRange:[adString rangeOfString:@"/"] withString:@"/$"];
}
cell.itemPrice.text = adString;
}
else if ([match valueForKey:@"REGULAR"] == [NSNull null])
{
cell.itemPrice.text = @"$ N/A";
}
else
{
NSNumberFormatter *currencyStyle = [[NSNumberFormatter alloc] init];
[currencyStyle setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyStyle setNumberStyle:NSNumberFormatterCurrencyStyle];
NSNumber *price = [NSNumber numberWithDouble:[[match valueForKey:@"REGULAR"] doubleValue]];
NSString *stringPrice = [currencyStyle stringFromNumber:price];
cell.itemPrice.text = [NSString stringWithFormat:@"%@", stringPrice];
}
if ([match valueForKey:@"AISLE"] == [NSNull null])
{
cell.itemLocation.text = @"Item location: N/A";
}
else
{
cell.itemLocation.text = [NSString stringWithFormat:@"Item Location: %@", [match valueForKey:@"AISLE"]];
}
match = nil;
}
}
return cell;
}
编辑 2: 这是 JSON 的一个片段:
{
items = {
263149 = {
AD = "###";
AISLE = 6A;
DESCRIPTION = "Cinnamon Toasters";
R = 9;
REGULAR = "#.##";
};
26599 = {
AD = "####";
AISLE = 6A;
DESCRIPTION = "Quaker Life Cereal";
R = 2;
REGULAR = "#.##";
};
40517 = {
AD = "###";
AISLE = 6A;
DESCRIPTION = "Toasted Oats";
R = 1;
REGULAR = "#.##";
};
};
};