在 NSPredicate 之后重新加载 tableview 数据后应用程序崩溃
App crashes after reloading tableview data after NSPredicate
UITableView
一开始完美加载数据。当我在 NSMutableDictionary
*list 上应用 NSPredicate
并且 table 重新加载数据时,在滚动之后应用程序崩溃了。我想主要问题出在 numberOfRowsInSection
。这是我的代码
ViewDidLoad
NSArray *allstartingPrice = @[@100,@200,@400,@500,@300,@100,@1000,@1000,@200,@700];
NSArray *alldistanceFrom = @[@7,@9,@8,@3,@30,@35,@50,@10,@15,@25];
NSAray *startingPrice = allstartingPrice;
NSAray *distanceFrom = alldistanceFrom;
list = [[NSMutableDictionary alloc] init];
[list setObject:startingPrice forKey:@"startingfrom"];
[list setObject:distanceFrom forKey:@"distance"];
这里是过滤数据的方法。
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
NSArray * allKeys = [list allKeys];
return [allKeys count];
}
- (void) sortTable{
int match = numberOfBudget;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF <= %i", match];
startingPrice = [allstartingPrice filteredArrayUsingPredicate:predicate];
int match2 = numberOfDistance;
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF <= %i", match2];
distanceFrom = [alldistanceFrom filteredArrayUsingPredicate:predicate2];
list = [[NSMutableDictionary alloc] init];
[list setObject:startingPrice forKey:@"startingfrom"];
[list setObject:distanceFrom forKey:@"distance"];
[self.tableView reloadData];
[self.tableView layoutIfNeeded];
}
数据过滤完美,但当 uitableviewscroll 到达 table 应用程序崩溃的末尾时。
其他方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;{
return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FSParallaxTableViewCell *cell = (FSParallaxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.cellImageView.image = [UIImage imageNamed:[[list objectForKey:@"imagesnames"] objectAtIndex:indexPath.row]];
cell.cellImageView.contentMode = UIViewContentModeScaleAspectFill;
cell.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:@"flat_color.png"];
cell.startingat.backgroundColor = [UIColor colorWithPatternImage:img];
cell.startingat.font = [UIFont fontWithName:@"HelveticaNeue-light" size:12];
cell.startingat.textColor = [UIColor blackColor];
cell.startingat.adjustsFontSizeToFitWidth = YES;
UIImage *img2 = [UIImage imageNamed:@"flat_color.png"];
cell.distance.backgroundColor = [UIColor colorWithPatternImage:img2];
cell.distance.textAlignment = NSTextAlignmentCenter;
cell.distance.font = [UIFont fontWithName:@"HelveticaNeue-light" size:9];
cell.distance.textColor = [UIColor whiteColor];
cell.distance.adjustsFontSizeToFitWidth = YES;
NSString *dis = [[NSString alloc] initWithFormat:@"%@ km", [[list objectForKey:@"distance"] objectAtIndex:indexPath.row]];
return cell;
}
错误日志
您面临的问题是因为您有两个不同的数组,并且都有不同的过滤器,这会导致问题,请尝试使用包含这样的字典对象的单个数组。所以像这样更改您的代码。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *allstartingPrice = @[@100,@200,@400,@500,@300,@100,@1000,@1000,@200,@700];
NSArray *alldistanceFrom = @[@7,@9,@8,@3,@30,@35,@50,@10,@15,@25];
array = [[NSMutableArray alloc] init];
for (NSInteger i=0; i<allstartingPrice.count; i++) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:[allstartingPrice objectAtIndex:i] forKey:@"startingfrom"];
[dic setValue:[alldistanceFrom objectAtIndex:i] forKey:@"distance"];
}
}
- (void) sortTable{
//int match = numberOfBudget;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startingfrom <= %d && distance <= %d", 100, 10];
filteredArray = [NSMutableArray arrayWithArray:[array filteredArrayUsingPredicate:predicate]];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (filteredArray.count > 0) {
return filteredArray.count;
}
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dic;
if (filteredArray.count > 0) {
dic = [filteredArray objectAtIndex:indexPath.row];
}
else {
dic = [array objectAtIndex:indexPath.row];
}
FSParallaxTableViewCell *cell = (FSParallaxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.cellImageView.image = [UIImage imageNamed:[[list objectForKey:@"imagesnames"] objectAtIndex:indexPath.row]];
cell.cellImageView.contentMode = UIViewContentModeScaleAspectFill;
cell.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:@"flat_color.png"];
cell.startingat.backgroundColor = [UIColor colorWithPatternImage:img];
cell.startingat.font = [UIFont fontWithName:@"HelveticaNeue-light" size:12];
cell.startingat.textColor = [UIColor blackColor];
cell.startingat.adjustsFontSizeToFitWidth = YES;
cell.startingat.text = [dic valueForKey:@"startingfrom"];
UIImage *img2 = [UIImage imageNamed:@"flat_color.png"];
cell.distance.backgroundColor = [UIColor colorWithPatternImage:img2];
cell.distance.textAlignment = NSTextAlignmentCenter;
cell.distance.font = [UIFont fontWithName:@"HelveticaNeue-light" size:9];
cell.distance.textColor = [UIColor whiteColor];
cell.distance.adjustsFontSizeToFitWidth = YES;
cell.distance.text = [dic valueForKey:@"distance"];
return cell;
}
UITableView
一开始完美加载数据。当我在 NSMutableDictionary
*list 上应用 NSPredicate
并且 table 重新加载数据时,在滚动之后应用程序崩溃了。我想主要问题出在 numberOfRowsInSection
。这是我的代码
ViewDidLoad
NSArray *allstartingPrice = @[@100,@200,@400,@500,@300,@100,@1000,@1000,@200,@700];
NSArray *alldistanceFrom = @[@7,@9,@8,@3,@30,@35,@50,@10,@15,@25];
NSAray *startingPrice = allstartingPrice;
NSAray *distanceFrom = alldistanceFrom;
list = [[NSMutableDictionary alloc] init];
[list setObject:startingPrice forKey:@"startingfrom"];
[list setObject:distanceFrom forKey:@"distance"];
这里是过滤数据的方法。
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section
{
NSArray * allKeys = [list allKeys];
return [allKeys count];
}
- (void) sortTable{
int match = numberOfBudget;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF <= %i", match];
startingPrice = [allstartingPrice filteredArrayUsingPredicate:predicate];
int match2 = numberOfDistance;
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF <= %i", match2];
distanceFrom = [alldistanceFrom filteredArrayUsingPredicate:predicate2];
list = [[NSMutableDictionary alloc] init];
[list setObject:startingPrice forKey:@"startingfrom"];
[list setObject:distanceFrom forKey:@"distance"];
[self.tableView reloadData];
[self.tableView layoutIfNeeded];
}
数据过滤完美,但当 uitableviewscroll 到达 table 应用程序崩溃的末尾时。 其他方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;{
return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FSParallaxTableViewCell *cell = (FSParallaxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.cellImageView.image = [UIImage imageNamed:[[list objectForKey:@"imagesnames"] objectAtIndex:indexPath.row]];
cell.cellImageView.contentMode = UIViewContentModeScaleAspectFill;
cell.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:@"flat_color.png"];
cell.startingat.backgroundColor = [UIColor colorWithPatternImage:img];
cell.startingat.font = [UIFont fontWithName:@"HelveticaNeue-light" size:12];
cell.startingat.textColor = [UIColor blackColor];
cell.startingat.adjustsFontSizeToFitWidth = YES;
UIImage *img2 = [UIImage imageNamed:@"flat_color.png"];
cell.distance.backgroundColor = [UIColor colorWithPatternImage:img2];
cell.distance.textAlignment = NSTextAlignmentCenter;
cell.distance.font = [UIFont fontWithName:@"HelveticaNeue-light" size:9];
cell.distance.textColor = [UIColor whiteColor];
cell.distance.adjustsFontSizeToFitWidth = YES;
NSString *dis = [[NSString alloc] initWithFormat:@"%@ km", [[list objectForKey:@"distance"] objectAtIndex:indexPath.row]];
return cell;
}
错误日志
您面临的问题是因为您有两个不同的数组,并且都有不同的过滤器,这会导致问题,请尝试使用包含这样的字典对象的单个数组。所以像这样更改您的代码。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSArray *allstartingPrice = @[@100,@200,@400,@500,@300,@100,@1000,@1000,@200,@700];
NSArray *alldistanceFrom = @[@7,@9,@8,@3,@30,@35,@50,@10,@15,@25];
array = [[NSMutableArray alloc] init];
for (NSInteger i=0; i<allstartingPrice.count; i++) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setValue:[allstartingPrice objectAtIndex:i] forKey:@"startingfrom"];
[dic setValue:[alldistanceFrom objectAtIndex:i] forKey:@"distance"];
}
}
- (void) sortTable{
//int match = numberOfBudget;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startingfrom <= %d && distance <= %d", 100, 10];
filteredArray = [NSMutableArray arrayWithArray:[array filteredArrayUsingPredicate:predicate]];
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (filteredArray.count > 0) {
return filteredArray.count;
}
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dic;
if (filteredArray.count > 0) {
dic = [filteredArray objectAtIndex:indexPath.row];
}
else {
dic = [array objectAtIndex:indexPath.row];
}
FSParallaxTableViewCell *cell = (FSParallaxTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.cellImageView.image = [UIImage imageNamed:[[list objectForKey:@"imagesnames"] objectAtIndex:indexPath.row]];
cell.cellImageView.contentMode = UIViewContentModeScaleAspectFill;
cell.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:@"flat_color.png"];
cell.startingat.backgroundColor = [UIColor colorWithPatternImage:img];
cell.startingat.font = [UIFont fontWithName:@"HelveticaNeue-light" size:12];
cell.startingat.textColor = [UIColor blackColor];
cell.startingat.adjustsFontSizeToFitWidth = YES;
cell.startingat.text = [dic valueForKey:@"startingfrom"];
UIImage *img2 = [UIImage imageNamed:@"flat_color.png"];
cell.distance.backgroundColor = [UIColor colorWithPatternImage:img2];
cell.distance.textAlignment = NSTextAlignmentCenter;
cell.distance.font = [UIFont fontWithName:@"HelveticaNeue-light" size:9];
cell.distance.textColor = [UIColor whiteColor];
cell.distance.adjustsFontSizeToFitWidth = YES;
cell.distance.text = [dic valueForKey:@"distance"];
return cell;
}