1

I have an applicatio in which i am using an nsmutablearray to controll a table view which i was being programatically created like this.`

tableview = [[UITableView alloc] init];
    [tableview setFrame:CGRectMake(self.view.frame.origin.x,44,self.view.frame.size.width, self.view.frame.size.height)];
    tableview.dataSource = self;
    tableview.delegate = self;

    [self.view addSubview:tableview];
     self.tableview.rowHeight = 55;


    [tableview release];

.then i have an nsmutable array which is an instance array.when it is first loading everything seems to be working fine.i am storing that mutable array to the nsuserdefaults.from the second time onwords i am trying to load the data from the nsuserdefaults array like this.

NSUserDefaults *userslist = [NSUserDefaults standardUserDefaults];
        NSArray *arrayObj = [userslist objectForKey:@"userslist"]; 

        NSMutableArray *searchfriendarray1=[NSMutableArray arrayWithArray:arrayObj];
        searchfriendarray=searchfriendarray1;

.thats also working .but in a methode i am trying to edit that array and then trying to reload the table the reload table is not getting called.`

[sortedFriendsArray removeAllObjects];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userslist"];
   [[NSUserDefaults standardUserDefaults]synchronize ];
   NSDictionary *dictionary = [self.searchfriendarray objectAtIndex:index];
     [dictionary setValue:@"offline" forKey:@"TYPE"];  

    NSUserDefaults *userslist = [NSUserDefaults standardUserDefaults];

    [userslist setObject:searchfriendarray forKey:@"userslist"];

`where sorted array was creating from my array after this .But when i call [self.tableview reloadData]; it is not reloading. Can anybody help me?

hacker
  • 8,919
  • 12
  • 62
  • 108
  • can we see the code in cellForRowAtIndexPath: and numberOfRowsInSection: – Aziz Feb 10 '13 at 08:03
  • Sorry: typing on a tablet! I meant to say As an aside i'm surprised you first batch of code works: i had to wrap an array up in a keyed archiver to store it in the user defaults. – Todd Feb 10 '13 at 08:12
  • @Todd i was not getting u? – hacker Feb 10 '13 at 12:50
  • Yup, I should definitely wait until I get to a proper keyboard! I wondered whether you are storing the array in NSUserDefaults correctly since you just seem to be grabbing the NSArray straight from userslist. I tried to do similar in my app and the array wasn't correctly stored for later use so had to use an NSKeyedArchiver. As I said, it's an aside to the main answer but might help in the long run. The first answer to this question has code to archive and un-archive an NSArray: http://stackoverflow.com/questions/537044/storing-custom-objects-in-an-nsmutablearray-in-nsuserdefaults. – Todd Feb 10 '13 at 15:00

2 Answers2

1

Two possible reason for having your tableview not updated ares

  • Your dataSource array is not updated correctly, so check the size / content of your array when you call reloadData

  • If your dataSource are updated correctly, probably you are calling reloadData not on the mainThread.

    [tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

oiledCode
  • 8,589
  • 6
  • 43
  • 59
0

Here are some points:

  • Declare your table view in header file (.h)
  • Alloc-Init table view in viewDidLoad method
  • Release table view in dealloc method

And do the same thing for your data source array updating, once array gets updated call table view reload data method:

[tableview reloadData];

Note: data source array = array which has been used in numberOfRows and cellForRowAtIndexPath method. This array requires to be updated before calling reload data method.

Hope this helps you

Mrunal
  • 13,982
  • 6
  • 52
  • 96