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

Brittany

When my app's user signs up for an account, the tableView starts out empty (as no data has been added by the user). 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;
                  }
Vidhyanand

You need to update your numberOfRows Delegate method

- (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..!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I stop an empty variable from being output?

How to stop NodeJS script from crashing when timeout exceeded

How can I prevent empty strings in my array from counting as a part of length?

How can I stop Tabula from automatically dropping empty columns?

How can I stop my UITableView from reusing cells?

How can I stop my page from refreshing when I submit a form without lagging my page like crazy?

How can I stop pages from scrolling with an empty hyperlink jump?

How can I stop gvim from crashing when opening a file?

I can't get my array from JSON to load into the UITableView

When embedding PageDown editor in my webpage, how I can I stop it from doing conversion in real time?

GridBagLayout, how can I stop my screen from stretching when I'm placing my components?

how can I stop multiline commands from messing up my cursor position when scrolling through history?

How can i stop my python program from crashing

How can I stop my controller from moving my mouse?

Stop app from crashing when submitting order with empty EditText

How can I stop LibreOffice from randomly crashing?

I have a UILabel that is visible only when my UITableView is empty. How can I center the text of that label in Swift?

how do i stop my application from crashing

How to stop the app crashing when String is empty

How can I stop ToolTips from inheriting my TextBlock style?

How can I make my caret stop from moving when clicking on dropdown menu?

How to stop an app from crashing when a button is clicked?

I can't figure out how to stop my navigation bar from highlighting my active link when I hover over it

How can I stop my array from resetting during run time?

How can I stop the app from crashing when I switch picker values?

How can I stop Powershell filtering from converting my array to string?

How can I prevent this from running on my screen reader when input is empty?

How can I make Spinner function stop crashing my app when trying to get the selected item?

How can I prevent my React app from crashing when I scroll to a non-indexed value in my array?