我在表视图中搜索数据时遇到此崩溃。我使用textfield作为搜索栏。这是我的代码:
NSMutableArray * searchArray;NSString * searchTextString;BOOL isFilter;@property ……
你替换你的 cellForRowAtIndexPath 用这个方法。
cellForRowAtIndexPath
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { static NSString *CellIdentifier = @"SubNotificationCell"; SubNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSDictionary *dict; if(isFilter) { // HERE was the problem. //dict = [[self->searchArray objectAtIndex:indexPath.row] objectForKey:@"Eqktx"]; dict = [self.searchArray objectAtIndex:indexPath.row] } else { dict = [self.EtNotifRepTableDataArray objectAtIndex:indexPath.row]; } NSString* notifValue = [dict objectForKey:@"Qmnum"]; NSString* equipNameValue = [dict objectForKey:@"Eqktx"]; NSString *values = @":"; cell.notifValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, notifValue]; cell.equipNameValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, equipNameValue]; return cell; }
问题是你直接在过滤器部分中获取对象 cellForRowAtIndexPath 返回NSString值的方法,然后再次在你试图获得 objectForKey 来自这个NSString 这导致崩溃,因为NSString没有已知的方法 objectForKey
objectForKey
编辑:
它与崩溃无关,但您还应该根据过滤器更新此方法。
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if(isFilter) { return [searchArray count]; } else { return [self.EtNotifRepTableDataArray count]; } }
尝试并分享您的结果。