當我的應用程序的用戶註冊一個帳戶時,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;
}
您實施必要的數據源方法「numberOfSectionsInTableView」 &'numberOfRowsInSection'?一個動態的tableView只會在有人告訴它的時候調用'cellForRowAtIndexPath' - 有意或無意使用這兩種數據源方法之一來顯示數據。檢查文檔 - https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html – smaura777
@ smaura777是的,請參閱上面的編輯(對不起,忘記包括這些)。 – Brittany