0

Building iOS application that connects to contacts application and manipulate them.

What I have

Tableview class that manages the cells, implements method that retrieve all the contact list from Addressbook, and display the array of contents in the table view cells. Imports Person.

DetailedView, when tapping on a cell it takes you to that detailed view where you can change the values of the data, name, phone, etc.

Class Person has properties that tableview imports to receive the address book contacts details, name, last name, etc.

What I want

When the user adds new contacts from the contact app (Apple's one), when bring the app to foreground again and make it active, the tableviewlist updates the view and lists all the contacts including the latest one added. Where is the best place to make sure that I always have updated my array & reloaded my tableview?

djikay
  • 10,450
  • 8
  • 41
  • 52
Moody
  • 352
  • 3
  • 15
  • 2
    Application did become active – Inder Kumar Rathore Jul 03 '14 at 10:46
  • 1
    If the table view is a subclass of `UITableView` it has a method `reloadData`, this will update the presentation of the table view. Trigger this method when you App enters foreground. – dasdom Jul 03 '14 at 10:47
  • I know the method **reloadData**, when i use it in viewWillAppear it works (when i am in detailed view and come navigate back to tableview), but when i use it in foreground or in app delegate in general, it doesn't seem to work. Of course i imported my tableview and all set. – Moody Jul 03 '14 at 11:03
  • I couldn't know whether you know `loadData`, because you didn't give me this information. Maybe you should have a look at http://stackoverflow.com/help/how-to-ask. – dasdom Jul 03 '14 at 11:08
  • Why do you feel offended because i said i know about the method? relax. My question mainly was "where", not "how". – Moody Jul 03 '14 at 11:12

3 Answers3

1

In application:didFinishLaunchingWithOptions: insert the following code to register for address book change notifications:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self);

Then implement the addressBookChanged method:

void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context) {
    UITableView *tableView = tableViewYouWantToUpdateOnABChange;
    [tableView reloadData];
}

For further details checkout the ABAddressBook reference

Manfred Scheiner
  • 390
  • 3
  • 10
  • how can i call self inside c addressBookChanged? it says undeclared Identifier. – Moody Jul 03 '14 at 15:13
  • You can't call self inside the c function, but you'll receive it as an argument to your function - see http://stackoverflow.com/a/24575767/1499194 – alivingston Jul 04 '14 at 13:49
0

You can trigger reload data of table view when method of containing viewer (either UIViewController or UITableViewController) viewWillAppear will be called. This will solve problem when you have detailed view opened and after updating data closing detailed view.

Premal
  • 551
  • 1
  • 6
  • 25
  • Yes, but it doesn't work if i am on the tableview, tap home button, go and add new contact in contacts app, then come back to my app's tableview – Moody Jul 03 '14 at 11:01
-1

Reload your tableview inside viewWillAppear method. Then place one more check in "applicationWillEnterForeground" method of your appdelegate file. Check the controller and tableview objects has there current instance. If so reload your table here too.

Let your controller is MyCont And make sure you have created property of your tableview object in controller class ok. Now Go to appdelegate.h and write

@property(nonatomic,retain) MyCont *cont;

Goto appdelegate.m and use

@synthesize cont;

Now goto viewDidLoad() method of MyCont class and write

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appdelegate.cont = self; 

Now goto applicationWillEnterForeground() of AppDelegate class and write

  if(cont)
    {
     [cont.tableviewobject reloadData]; 
    }

By doing all this your tableview datasource metods will be fired.

Yogendra
  • 1,728
  • 16
  • 28
  • It doesn't work on "applicationWillEnterForeground", i must be missing something. I will tell you what i am doing. In AppDelegate, i import my tableviewcontroller class, create new instance in applicationDidFinishLaunchingWithOptions, and in applicationWillEnterForeground i call on reloadData with my instance of TableViewController – Moody Jul 03 '14 at 11:51
  • What i mean by it doesn't work is that, my array of address book contacts gets updated, but my tableview doesn't change. – Moody Jul 03 '14 at 11:57
  • Because you have created new instance of tableviewcontroller. Make property of controller object in appdelegate (MyController *cont; ) and initialize it in controller class with self. Then it will work. – Yogendra Jul 03 '14 at 12:02
  • I am sorry, can you write more details, because i never had to do that before i got confused. I am sorry but please explain further -feel free to write code :) - Thank you again! – Moody Jul 03 '14 at 12:11
  • It still didn't work, i am not sure exactly what is the reason, there must be something i don't understand.. Thank you you clarify very well i just not experienced enough and its first time to deal with such issue of updating views. – Moody Jul 03 '14 at 12:42
  • Is there a way you can look at my code? I really need to understand what i am doing wrong because not just to make this small thing work, but to have this in my head forever and learn something from it. – Moody Jul 03 '14 at 12:51
  • It worked!, what does this line do AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; – Moody Jul 03 '14 at 13:15
  • It makes shared instance of appDelegate class. This only class you can use in all your project classes. Other classes need allocation. – Yogendra Jul 03 '14 at 13:17
  • THANK YOU! i got it 100% – Moody Jul 03 '14 at 13:47