2015-05-14 18 views
0

當我的應用程序的用戶註冊一個帳戶時,tableView開始爲空(因爲用戶沒有添加任何數據)。當即將填充的數組爲空時,如何阻止應用程序崩潰?到目前爲止,我已經試過顯示視圖時,表/數組是空的,但是崩潰仍然出現...如何在數組爲空時阻止我的UITableView崩潰?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [self.storageData count]; 

} 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell"; 

     StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier]; 
     if (cell == nil) 

      { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil]; 
      cell = [nib objectAtIndex:0]; 
      } 

     if (self.storageData > 0) { 

      noitemsView.hidden = YES; 

       NSDictionary *tmp = [self.storageData objectForKey:[NSString stringWithFormat:@"%d",(long)indexPath.row]]; 


      [[cell itemName] setText:(NSString *)[tmp objectForKey:@"title"]]; 

        NSString *title = [tmp objectForKey:@"title"]; 
        [[cell itemName] setText:title]; 


       NSDictionary *node = [self.descripData objectAtIndex:indexPath.row]; 
       [[cell itemDescrip] setText:[node objectForKey:@"body"]]; 
       NSLog(@"%@", self.descripData); 

       NSString *secondLink = [[self.descripData objectAtIndex:indexPath.row] objectForKey:@"photo"]; 

       [cell.itemPhoto sd_setImageWithURL:[NSURL URLWithString:secondLink]]; 

       NSLog(@"%@",secondLink); 


        } 
     else { 
      noitemsView.hidden = NO; 
     } 
        return cell; 
        } 
+0

您實施必要的數據源方法「numberOfSectionsInTableView」 &'numberOfRowsInSection'?一個動態的tableView只會在有人告訴它的時候調用'cellForRowAtIndexPath' - 有意或無意使用這兩種數據源方法之一來顯示數據。檢查文檔 - https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html – smaura777

+0

@ smaura777是的,請參閱上面的編輯(對不起,忘記包括這些)。 – Brittany

回答

0

你需要更新你的numberOfRows委託方法

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if ([self.storageData count] > 0 && self.descripData.count>0) 
    { 
     return [self.storageData count]; 
    } 
    else 
    return 1; 
} 

而且修改代碼如下

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
---------- 
     if (self.storageData.count > 0 && self.descripData.count>0) { 
------------//Instead of self.storageData>0 
     } 
---------- 
} 

它修復了崩潰問題,如果storageData是空的..!鑑於

+0

試過這個,崩潰仍然發生。這就是說,我是否也需要對我的descripData數組也做同樣的事情? storageData包含單元格中的標題,而descripData包含單元格中的描述。雖然我想如果storageData.count是空的,它將返回0。我得到的錯誤是:終止應用程序由於未捕獲的異常'NSRangeException',原因:'*** - [__ NSArrayI objectAtIndex:]:索引0超出空數組的邊界' – Brittany

+0

我更新了我的答案..檢查它。它正在檢查storageData和descripData數組是否爲空。 – Vidhyanand

+0

嗯不錯 - 它的工作原理;非常感謝!! :) – Brittany

0

這是不是真的正確,但由於您的問題是,特別是約使應用程序不崩潰,你可以這樣做:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (self.storageData) 
     return [self.storageData count]; 
    else 
     return 1; 
} 

這意味着如果數組不爲空,則顯示數組中的元素數。否則,只顯示1(或任何你想要的默認數字)。

此外,在的cellForRowAtIndexPath,你需要做相同的檢查:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *DoctorsTableIdentifier = @"StorageItemTableViewCell"; 

    StorageItemTableViewCell *cell = (StorageItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:DoctorsTableIdentifier]; 
    if (cell == nil) 

     { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StorageItemTableViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
     } 

    if (self.storageData) // <-- check for nil values first 
     { if (self.storageData > 0) { 
    //more code 
    } 

同樣,不好還是正確的,但它使應用程序不會崩潰,這是你問什麼。

0
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
//If "numberOfRowsInSection" return '0' or nil, "cellForRowAtIndexPath" should not be called. 
//Are you sure the "self.storageData.count" is empty? 
if (_storageData == nil) 
{ 
    return 0; 
} 
else 
{ 
    return _storageData.count; 
} 
} 
+0

試過這個,我仍然拋出以下錯誤:終止應用程序由於未捕獲異常'NSRangeException',原因:'*** - [__ NSArrayI objectAtIndex:]:索引0超出空數組的界限' – Brittany

0

這裏有一個問題:

if (self.storageData > 0) { 

     noitemsView.hidden = YES; 
     .... 

您比較self.storageData>而不是0 self.storageData計數> 0,因此,如果總爲真,這是始終嘗試使用不可用的數據創建單元格。

0

請嘗試以下操作。

self.tableView.dataSource = self.storageData ? self : nil; 
0

寫代碼做負載

self.storageData = [[NSMutableArray alloc] init]; 
-1

您需要檢查數組中的行數計數部分方法:1。

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
if(!myArray.count) 
    return 0; 

    return [myArray.count]; 
}