ios - UITableView 在滚动时得到空项目

标签 ios objective-c uitableview null cell

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

我的代码:

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

截图:

enter image description here

有哪位大神可以指教一下。提前致谢。

最好的问候。

最佳答案

我解决了。我是 objective-c 的菜鸟,我需要更多地了解(强、弱、保留、分配)。我只是在我的联系人类中将 weak 替换为 retain:

@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 = 保留:

  • 它说“在我不再指向它之前将其保存在堆中” 换句话说“我是所有者,你不能在瞄准之前解除分配 与保留相同”

:

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

来源: Objective-C ARC: strong vs retain and weak vs assign

现在完美运行!

enter image description here

谢谢,问候。

关于ios - UITableView 在滚动时得到空项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422356/

相关文章:

ios - UIPanGestureRecognizer 在触摸时立即做一些事情

ios - 表格部分标题上方的白色分隔符

ios - UITableViewCell 中的 UIButton 被重用

ios - 将 scrollOffset 保存到 didEndDisplayingCell 中的 UITableView 数据源未正确出列

ios - NSFetchedResultsController 尝试将第 1 行插入第 0 节,但在使用 userInfo (null) 更新后,第 0 节中只有 0 行

iphone - UITableView 中的一行中有两个单元格

ios - 如何在 Xcode 4.3 上查看复制到构建文件夹的资源?

objective-c - 如何使用 XCode 4 使我的代码兼容 Tiger (10.4)?

ios - 使用 dequeueReusableCellWithIdentifier 指定单元格时停止影响随机 UITableViewCells

iphone - 在iPhone应用程序中显示N深度的分层数据的示例代码