0

我有一个小应用程序,我试图从 sqlite 数据库中加载 UITableView 中的一些数据。显然一切正常,但是当我尝试滚动时,我得到空数据。

我的代码:

    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;
    }

截屏:

在此处输入图像描述

如果有人可以启发我。提前致谢。

此致。

4

2 回答 2

3

我解决了。我是objective-c的菜鸟,我需要了解更多关于(强、弱、保留、分配)的知识。我只是在我的联系人类中用保留替换弱:

@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;

强=保留

  • 它说“将其保留在堆中,直到我不再指向它为止”,换句话说“我是所有者,您不能在使用与保留相同的目标之前解除分配”

  • 它说“只要其他人强烈指出它,就保留它”

资料来源: Objective-C ARC:强 vs 保留和弱 vs 分配

现在完美运行!

在此处输入图像描述

无论如何,谢谢,问候。

于 2013-10-17T10:07:04.717 回答
0

尝试这个,

- (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;
}
于 2013-10-17T09:18:52.477 回答