I am trying to remove all the objects from the NSMutableArray and I am reloading the UITableView. But it produces SIGABRT in cellForRowAtIndexPath. The code is given below.
if ([nsMutableArray count] != 0) {
[nsMutableArray removeAllObjects];
[tableView reloadData];
}
It shows SIGABRT in
if (cell == nil) {
cell = (UICustomCell *)[nsMutableArray objectAtIndex:indexPath.row];
}
Help me to come out of this problem.
change this
to
if ([NSMutableArrayObject count] != 0) {
[NSMutableArrayObject removeAllObjects];
[tableView reloadData];
}
if (cell == nil) {
cell = (UICustomCell *)[NSMutableArrayObject objectAtIndex:indexPath.row];
}
You are calling instance methods (count, removeAllObjects, reloadData, objectAtIndex:) on classes (NSMutableArray, UITableView) instead of object instances.
Related
i want to push next controller,but unsuccess.what should i do?
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
NSInteger section = indexPath.section, row = indexPath.row;
if (section == 1) {
if (row == 0) {
DetailsViewController * DetailsVC = [DetailsViewController alloc]init];
[self.navigationController pushViewController: DetailsVC animated:YES];
} else if (row == 1) {
}
} else if
(section == 2) {
}
}
you have to set identifier for viewcontroller and call the method like
CallViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:#"CallViewController"];
[self.navigationController pushViewController:vc animated:YES];
and be sure you had embedded Navigation View Controller.
I use iOS SDK 8 and having problem to display annotation for certain locations but it is working fine for some locations.
For those failed location, it keeps looping at if (pinView == nil) and the app will quit unexpectedly, any idea for the root caused?
- (void)populateAllLocation {
self.locations = [selectedCountry_.locations allObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"latitude != nil && longitude != nil"];
self.locations = [[selectedCountry_.locations filteredSetUsingPredicate:predicate] allObjects];
[mapView_ addAnnotations:locations_];
[self centralizeMap];
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:MKUserLocation.class]) {
static NSString *PinIdentifier = #"UserLocationPin";
MKAnnotationView *pinView =
[mapView_ dequeueReusableAnnotationViewWithIdentifier:PinIdentifier];
if (pinView == nil) {
pinView =
[[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:PinIdentifier] autorelease];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"mylocation_pin"];
}
[self populateAllLocation];
return pinView;
}
Your populateAllLocation is triggering display of the annotation views, but the delegate method itself calls populateAllLocation so you have a loop. You should only be returning annotation views in the delegate, not altering the map.
Please direct me to the right way.
I implemented this code to fetch my objects:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSPredicate *predicate = nil;
if (self.selectedCategory)
{
predicate = [NSPredicate predicateWithFormat:#"ANY category_ids.category_id == %#", self.selectedCategory];
}
_fetchedResultsController = [EyeArtist fetchAllGroupedBy:nil withPredicate:predicate sortedBy:#"artist_id" ascending:NO delegate:self];
return _fetchedResultsController;
}
So when app run the at first time fetch works without predicate, so at second time I need new fetch with predicate.
I tap on the button and set string self.selectedCategory, but I don't know how to refetch data from - (NSFetchedResultsController *)fetchedResultsController;
So I suppose it has to be like execute new request for fetchedResultsController instance.
After changing the search criteria, you have to set the instance variable self.fetchedResultsController to nil,
so that the next call to the "lazy getter" function creates a new FRC with the
changed predicate. Something like this:
self.fetchedResultsController = nil;
[self.fetchedResultsController performFetch:&error];
[self.tableView reloadData];
This is the pattern I use for where a fetch controller needs a property:
- (void)setSelectedCategory:(id)selectedCategory{
if(selectedCategory == _selectedCategory){
return _selectedCategory
}
_selectedCategory = selectedCategory;
self.fetchedResultsController = nil;
if(self.isViewLoaded){
[self.tableView reloadData]; // but better to put this in an update views method that you can also call from viewDidLoad.
}
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
id selectedCategory = self.selectedCategory;
// Only need this if a category is required.
if(!selectedCategory){
return nil;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"ANY category_ids.category_id == %#", selectedCategory];
_fetchedResultsController = [EyeArtist fetchAllGroupedBy:nil withPredicate:predicate sortedBy:#"artist_id" ascending:NO delegate:self];
return _fetchedResultsController;
}
I need some help searching / filtering a Core data entity. The results array returns Null..
I have a root view with a search bar, controller and tableview. This view shows normally.
I'm calling the UISearchBarDelegate and the UISearchDisplayDelegate.
I have a mutable array (searchResults).
My search code is as follows:
-(void) filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSLog(#"%s", __FUNCTION__);
[self.searchResults removeAllObjects];
for (Entity *ent in [self.fetchedResultsController fetchedObjects])
{
if ([scope isEqualToString:#"All"] || [ent.title isEqualToString:scope])
{
NSRange range = [ent.title rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound)
{
NSLog(#"Adding title '%#' to searchResults as it contains '%#'", ent.title, searchText);
[self.searchResults addObject:ent];
}
}
}
NSLog(#"The searchResults array contains '%#'", searchResults); <<<< RETURNS NULL
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:#"All"];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text ]scope:#"All"];
return YES;
}
and the cell config code is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyVeryOwnCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Entity *entity = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
//NSLog(#"Configuring cell to show search results");
entity = [self.searchResults objectAtIndex:indexPath.row];
}
else
{
//NSLog(#"Configuring cell to show normal data");
entity = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
cell.textLabel.text = entity.title;
return cell;
}
I must be doing something dumb as the searchResults array appears to be null. I would appreciate any advice.
You just forgot to alloc/init the searchResults array.
;-)
I followed a tutorial I found online to create a tableview with sections and an index from an array of custom objects. This code works with the exception that when I select a row in the table I the index path for that section and not for the entire array. I can see why it doesn't work but I can't figure out how to address the fix, this is my cell for tableview code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"NameCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int displayOrder = [defaults integerForKey:#"displayOrder"];
int sortOrder = [defaults integerForKey:#"sortOrder"];
NSString *alphabet = [listIndex objectAtIndex:[indexPath section]];
NSPredicate *sectionPredicate = [[NSPredicate alloc] init];
if (sortOrder == 1) {
//NSLog(#"fName is predicate at cell level");
sectionPredicate = [NSPredicate predicateWithFormat:#"fName beginswith[c] %#", alphabet];
} else {
//NSLog(#"lName is predicate at cell level");
sectionPredicate = [NSPredicate predicateWithFormat:#"lName beginswith[c] %#", alphabet];
}
NSArray *sectionContacts = [filteredList filteredArrayUsingPredicate:sectionPredicate];
if (isSearching) {
current = [filteredList objectAtIndex:indexPath.row];
} else{
current = [sectionContacts objectAtIndex:indexPath.row];
}
if (displayOrder == 1) {
NSString *fullName = [NSString stringWithFormat:#"%# %#",[current valueForKey:#"fName"],[current valueForKey:#"lName"]];
[cell.textLabel setText:fullName];
//NSLog(#"FirstNameFirst");
} else {
NSString *fullName = [NSString stringWithFormat:#"%# %#",[current valueForKey:#"lName"],[current valueForKey:#"fName"]];
[cell.textLabel setText:fullName];
//NSLog(#"LastNameFirst");
}
[cell.detailTextLabel setText:[current valueForKey:#"extension"]];
return cell; }
THen I call the segue with this code.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"showContact"]) {
DetailViewController *dvc = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSDictionary *c = [filteredList objectAtIndex:path.row];
[dvc setCurrentContact:c];
[searchBar resignFirstResponder];
} }
The problem is that the objectAtIndex:path.row returns the index for that section but it isn't modified for the entire array, so if a name in the "B" section that is at index 4 of that section is tapped it returns the object at index 4 of the primary array. I have been scratching my head to figure out how to get the index for the full array and not for the one that is only local to that section.
I'll buy you a 6 pack of your favorite beverage if you can help!
Thanks!
You do it the same way that they do it in the first function, so change your prepareForSegue to this:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showContact"]) {
DetailViewController *dvc = [segue destinationViewController];
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSDictionary *c;
NSString *alphabet = [listIndex objectAtIndex:[path section]];
NSPredicate *sectionPredicate = [[NSPredicate alloc] init];
if (sortOrder == 1) {
sectionPredicate = [NSPredicate predicateWithFormat:#"fName beginswith[c] %#", alphabet];
} else {
sectionPredicate = [NSPredicate predicateWithFormat:#"lName beginswith[c] %#", alphabet];
}
NSArray *sectionContacts = [filteredList filteredArrayUsingPredicate:sectionPredicate];
if (isSearching) {
c = [filteredList objectAtIndex:path.row];
} else{
c = [sectionContacts objectAtIndex:path.row];
}
[dvc setCurrentContact:c];
[searchBar resignFirstResponder];
}
}
Note that it would probably be best to pull the common code out and make a separate function instead of using it twice like this.