简体   繁体   English

当数组为空时,如何防止UITableView崩溃?

[英]How can I stop my UITableView from crashing when the array is empty?

When my app's user signs up for an account, the tableView starts out empty (as no data has been added by the user). 当我的应用程序的用户注册一个帐户时,tableView开始为空(因为用户未添加任何数据)。 How can I stop the app from crashing when the soon-to-be populated array is empty? 当即将填充的数组为空时,如何阻止应用程序崩溃? So far, I've tried displaying a view when the table/array is empty, but the crash still occurs... 到目前为止,我尝试过在表/数组为空时显示视图,但是仍然会发生崩溃...

- (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;
                  }

You need to update your numberOfRows Delegate method 您需要更新您的numberOfRows委托方法

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

Also modify the code as below 还要如下修改代码

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

It fixes the crash issue if storageData is empty..! 如果storageData为空,它可以解决崩溃问题。

This is not really correct, but since your question is specifically about making the app not crash, you could do this: 这并不是真的正确,但是由于您的问题专门是关于使应用程序不崩溃,因此您可以执行以下操作:

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

This means that if the array is not empty, display the number of elements in the array. 这意味着,如果数组不为空,则显示数组中的元素数。 Else, display just 1 (or whatever default number you want). 否则,仅显示1(或您想要的任何默认数字)。

Also, in cellForRowAtIndexPath, you need to make the same check: 另外,在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
    }

Again, not good or correct, but it makes the app not crash, which is what you asked about. 再次,这不是好还是不正确,但这会使应用程序崩溃,这是您所要的。

- (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;
}
}

Here is a problem: 这是一个问题:

if (self.storageData > 0) {

        noitemsView.hidden = YES;
        ....

You are comparing self.storageData > 0 instead of [self.storageData count] > 0, so the if is always evaluating to true, and it is always trying to create a cell with data that is not available. 您正在比较self.storageData> 0而不是[self.storageData count]> 0,因此if始终求值为true,并且始终尝试使用不可用的数据创建单元格。

尝试以下方法。

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

鉴于确实写了代码

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

You need to check array count in number of row in section method: 1. 您需要在节方法中检查行数中的数组计数:1。

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

   return [myArray.count];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 阻止Tableview在数组为空时崩溃? - Stop Tableview from crashing when array is empty? 如何阻止我的快速应用程序崩溃 - How can I stop my swift app from crashing 订阅NSNotificationCenter时如何防止我的应用程序崩溃? - How can I stop my app from crashing when subscribing to NSNotificationCenter? 如何通过清除删除确认按钮停止UITableView上的reloadData? - How can I stop reloadData on my UITableView from clearing the delete confirmation button? 我有一个 UILabel 仅当我的 UITableView 为空时才可见。 如何在 Swift 中将该标签的文本居中? - I have a UILabel that is visible only when my UITableView is empty. How can I center the text of that label in Swift? 我无法从JSON获取数组以加载到UITableView中 - I can't get my array from JSON to load into the UITableView 取消选中UITableview中的单元格时,如何从数组中删除数据? - How can I remove data from the array when uncheck the cell in UITableview? 如何用JSON数组中的数据填充UITableView? - How do I populate my UITableView with data from JSON array? 在数组为空时如何避免从API提取数据时应用崩溃 - How to avoid app crashing when pulling data from API when array is empty 如何安全地在UITableView中使用未初始化的数组并显示一个空表 - How can I safely use an un-initialized array in a UITableView and show an empty table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM