0

I have a small application, where i'm trying to load some data in UITableView from a sqlite database. Apparently everything works fine, but when I try to scroll i get Null data.

My code:

    self.tblContacts.dataSource = self;
    self.tblContacts.delegate = self;

    self->mainManager = [[MainManager alloc] init: [NSString stringWithFormat:@"%@/contacts.db", resourcePath]];
    self->contactList = [[ContactManager alloc] init];
    self->contactList = [mainManager loadContacts];
    -----------------------------------------------------


    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }

    - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [self->contactList getContactsCount];
    }

    - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellID = @"Cell";
        CustomCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellID];

        if (!Cell)
            Cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];

        Cell.lblCompany.text = [[self->contactList getContact:indexPath.row] company];
        Cell.lblContact.text = [NSString stringWithFormat:@"%@ %@", [[self->contactList getContact:indexPath.row] name], [[self->contactList getContact:indexPath.row] surname]];

        return Cell;
    }

ScreenShot:

enter image description here

If anyone can enlighten me please. Thanks in advance.

Best regards.

SamYan
  • 1,449
  • 1
  • 17
  • 36
  • did u check the data in self->contactList array ? .Find out what [self->contactList getContact:3] value is ? – Benny Dalby Oct 17 '13 at 09:34
  • It seems the problem is MainManager class, as I'm trying to get the data from a clicking in button, and I also returns null. – SamYan Oct 17 '13 at 09:44

2 Answers2

3

I solved it. I'm noob in objective-c and i need to learn more about (strong, weak, retain, assign). I just replace weak by retain in my Contact Class:

@property (retain) NSString *name;
@property (retain) NSString *surname;
@property (retain) NSString *company;
@property (retain) NSString *cif;
@property (retain) NSString *email;
@property (retain) NSString *address;
@property (retain) NSString *city;
@property (retain) NSString *telephone;
@property (retain) NSString *provider;

strong = retain:

  • it says "keep this in the heap until I don't point to it anymore" in other words " I'am the owner, you cannot dealloc this before aim fine with that same as retain"

weak:

  • it says "keep this as long as someone else points to it strongly"

Source: Objective-C ARC: strong vs retain and weak vs assign

Now works perfectly!

enter image description here

Thanks anyway, regards.

Community
  • 1
  • 1
SamYan
  • 1,449
  • 1
  • 17
  • 36
0

Try this,

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

 static NSString *CellID = @"Cell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellID];

    if (cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects)
        {
            if ([currentObject isKindOfClass:[UITableViewCell class]])
            {
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

        Cell.lblCompany.text = [[self->contactList getContact:indexPath.row] company];
        Cell.lblContact.text = [NSString stringWithFormat:@"%@ %@", [[self->contactList getContact:indexPath.row] name], [[self->contactList getContact:indexPath.row] surname]];

        return Cell;
}
karthika
  • 4,075
  • 3
  • 19
  • 23