2

In my app I have a UITableViewCobtroller which creates the table view with checkmark accessory type. Table View loads and works correctly. In didSelectRowAtIndex method I wrote method of adding data in sqlite dataBase:

        [self.dataBaseController openSettingsDB];
        [self.dataBaseController updateRecordIntoTableNamed:kTableName withField:kSField1Name fieldValue:newCell.textLabel.text];
        [self.dataBaseController closeDB];

It works well. So what I want is to retrieve the recorded data from dataBase and when the application is relaunched to select the row, that has the title, that I retrieved from sqlite dataBase.

I tried this:

-(void)viewDidLoad
{
    NSArray *array = [[NSArray alloc] initWithObjects:
                      kCourse1, kCourse2, kCourse3, kCourse4, kCourse5, kCourse6, nil];
    self.list = array;
    [self.dataBaseController openSettingsDB];
    [self.dataBaseController getRowFromTableNamed:kTableName whichRow:kSField1Name];
    self.chosenStr = self.dataBaseController.dataString;
    [lastIndexPath indexAtPosition:[array indexOfObjectIdenticalTo:self.chosenStr]];
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell = [self.tableView cellForRowAtIndexPath:lastIndexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [self.tableView reloadData];
    [array release];
    [super viewDidLoad];
}

But it doesn't work. Please, suggest me any ideas. Thanks in advance.

Alick

Alick Dikan
  • 65
  • 2
  • 3
  • 10
  • 1
    Right off the bat, I noticed that the first snippet uses kTableName as the table name, and the second snippet uses kSettingsTableName, which could be the cause of the problem. Beyond that, we might need a bit more detail to go on. – BP. Oct 24 '11 at 13:28
  • 1
    [super viewDidLoad];put as first statement in viewDidLoad – Narayana Rao Routhu Oct 24 '11 at 13:29

2 Answers2

4

Updated Answer:

You should not be doing this in viewdidLoad in the first place. Its a bad practice, you should do it in cellForRowAtIndexPath:.

More explaination:

The table is not loaded as of yet in viewDidLoad. You have to do [self.tableView reloadData]; before doing anything else. Doing that will call the delegate methods for table (it has no idea how many cells there are so getting the cell for any specfic index path doesn't make sense). See Table View Programming guide.

Also:

UITableViewCell *cell = [[UITableViewCell alloc] init];
cell = [self.tableView cellForRowAtIndexPath:lastIndexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;

First, this is wrong. You are leaking memory with that alloc/init. Just do:

UITableViewCell *cell = nil;
cell = [self.tableView cellForRowAtIndexPath:lastIndexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
zakishaheen
  • 5,551
  • 1
  • 22
  • 29
  • 1
    The whole (correct) answer could be boiled down to the last sentence, but +1 for the extra info anyway! – jrturton Oct 24 '11 at 13:34
1

You need a call to UITableView's - scrollToRowAtIndexPath:atScrollPosition:animated: method. I would put this the viewDidAppear: method of your UITableViewController. See UITableView Class Reference.

odrm
  • 5,149
  • 1
  • 18
  • 13