我有一个小应用程序,我试图从一个 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;
}
截图:
有哪位大神可以指教一下。提前致谢。
最好的问候。
最佳答案
我解决了。我是 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
现在完美运行!
谢谢,问候。
关于ios - UITableView 在滚动时得到空项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19422356/