滚动时UITableView获取了空项

时间:2013-10-17 09:04:48

标签: 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

如果有人可以赐教我。提前谢谢。

最好的问候。

2 个答案:

答案 0 :(得分: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;

strong =保留

  • 它说“把它保存在堆中,直到我不再指向它” 换句话说“我是主人,你不能在瞄准之前解决这个问题 与保留“
  • 相同

<强>弱

  • 它说“只要别人强烈指出它就保持这个”

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

现在完美无缺!

enter image description here

非常感谢,问候。

答案 1 :(得分: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;
}