2013-10-17 58 views
0

我有一個小應用程序,我試圖從sqlite數據庫加載一些數據在UITableView中。顯然一切工作正常,但當我嘗試滾動時,我得到空數據。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; 
    } 

截圖:

enter image description here

如果有人能賜教請。提前致謝。

此致敬禮。

+0

你是否在自檢> contactList數組中檢查數據?找出[self-> contactList getContact:3]的值是什麼? –

+0

看來問題是MainManager類,因爲我試圖從點擊按鈕中獲取數據,而且我也返回null。 –

回答

3

我解決它。我在objective-c中是noob,我需要了解更多(強,弱,保留,分配)。我剛剛更換通過弱我聯繫類別保留

@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: strong vs retain and weak vs assign

現在完美的作品!

enter image description here

還是要謝謝你,問候。

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

感謝您的迴應,但我試過了,結果相同...滾動時出現空項目。 –