2021年2月10日星期三

Obj-c - App crashing if tableview section is empty?

I'm using the below code to alphabetically organize my tableView. The filter works correctly, however if one of the sections contains no results (ie. There's nothing returned for the letter "A"), it causes my app to crash, as the tableView is trying to populate a cell with data that isn't there. Any idea how I can prevent this?

ViewController.m

 - (void)viewDidLoad {          [super viewDidLoad];                    self.clientSections = [NSArray arrayWithObjects:@"#", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", nil];            }            -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {                    if (tableView == self.tableView) {                            return 27;                        } else {                    return 0;      }      }        - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView      {         return self.clientSections;      }            - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index      {         return index;      }            - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {         return [self.clientSections objectAtIndex:section];      }                  -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {                          if (self.clients.count > 0) {                            self.finalfiltered = [self.clients filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(%K beginswith[cd] %@)", @"last name", [self.clientSections objectAtIndex:section]]];                                      return [self.finalfiltered count];                        }                         }            -(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {                                 static NSString *ClientTableIdentifier = @"ClientTableViewCell";                            ClientTableViewCell *cell = (ClientTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:ClientTableIdentifier];                            if (cell == nil)              {                  NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ClientTableViewCell" owner:self options:nil];                  cell = [nib objectAtIndex:0];                                }                                 NSString *photo = [self.finalfiltered valueForKey:@"client photo"][indexPath.row];                     NSString *first = [self.finalfiltered valueForKey:@"first name"][indexPath.row];                       NSString *last = [self.finalfiltered valueForKey:@"last name"][indexPath.row];                      NSString *telephone = [self.finalfiltered valueForKey:@"phone"][indexPath.row];                      NSString *fullName = [NSString stringWithFormat:@"%@ %@", first, last];                  cell.clientName.text = fullName;          cell.subtext.text = telephone;                        NSURL *imageUrl = [NSURL URLWithString:photo];          NSLog(@"The photo url is %@", photo);          [cell.clientPhoto setImageWithURL:imageUrl];                            return cell;                                          }                                    }  

Edit: Here's what is returned when I log self.finalfiltered. Note, I'm sorting by last name. Order appears to be correct.

2021-02-10 16:53:34.905601-0800 [1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.905846-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.906076-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.906335-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.906566-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.906772-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.907025-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.907558-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.907772-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.908296-0800 -[1784:532468] THE FINAL FILTER (          {          body = none;          city = Victoria;          "client photo" = "/stockphotos/person5.png";          country = Canada;          email = "thomas.ingram@outlook.com";          "first name" = Thomas;          "last name" = Ingram;          "mailing address" = "502 Catherine Street";          "mailing address 2" = "Apt 403";          nid = 127;          "node_title" = "Thomas Ingram";          notes = "test notes";          phone = "250-998-0389";          scheduleddate = "Feb 8 2021 1:00 PM";          scheduledtime = none;            "state or province" = BC;          "zip code" = "V9A 3T3";      }  )  2021-02-10 16:53:34.908833-0800 -[1784:532468] THE FINAL FILTER (          {          body = none;          city = Toronto;          "client photo" = "/stockphotos/person2.png";          country = Canada;          email = "andrea@gmail.com";          "first name" = Andrea;          "last name" = Johnson;          "mailing address" = "227 Willow Avenue";          "mailing address 2" = "Unit 2034";          nid = 124;          "node_title" = "Andrea Johnson";          notes = "test notes";          phone = "416-223-2397";          scheduleddate = "Feb 8 2021 07:00 PM";          scheduledtime = "5:45 PM";          "state or province" = ON;          "zip code" = "M5M 1W4";      }  )  2021-02-10 16:53:34.909541-0800 -[1784:532468] THE FINAL FILTER (          {          body = none;          city = Burnaby;          "client photo" = "/sites/default/files/stored/1612926759.jpg";          country = Canada;          email = email@email.com;          "first name" = Cody;          "last name" = Lin;          "mailing address" = "4036 Pandora Street";          "mailing address 2" = "-";          nid = 171;          "node_title" = "Cody Lin";          notes = "test notes";          phone = "604-250-7422";          scheduleddate = none;          scheduledtime = none;          "state or province" = BC;          "zip code" = V5C2A9;      }  )  2021-02-10 16:53:34.910439-0800 -[1784:532468] THE FINAL FILTER (          {          body = none;          city = "New York City";          "client photo" = "/stockphotos/person4.png";          country = US;          email = "mlevy39@gmail.com";          "first name" = Michael;          "last name" = Levy;          "mailing address" = "22 Lexington Avenue";          "mailing address 2" = "Apt 102";          nid = 126;          "node_title" = "Michael Levy";          notes = "test notes";          phone = "212-983-0029";          scheduleddate = "Feb 10 2021 1:00 PM";          scheduledtime = none;          "state or province" = NY;          "zip code" = 90020;      },          {          body = none;          city = London;          "client photo" = "/stockphotos/person1.png";          country = Canada;          email = "janinejlohr@gmail.com";          "first name" = Janine;          "last name" = Monroe;          "mailing address" = "909 Fake Street";          "mailing address 2" = "Unit 4103";          nid = 123;          "node_title" = "Janine Monroe";          notes = "test notes";          phone = "778-028-2938";          scheduleddate = "Feb 14 2021 7:37 PM, Feb 17 2021 9:00 AM";          scheduledtime = none;          "state or province" = BC;          "zip code" = "V6E 4V2";      }  )  2021-02-10 16:53:34.911207-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.911423-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.911630-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.911834-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.912033-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.912233-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.912431-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.913457-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.913671-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.913931-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.914378-0800 -[1784:532468] THE FINAL FILTER (          {          body = none;          city = Vancouver;          "client photo" = "/stockphotos/person3.png";          country = Canada;          email = "cali.wright@gmail.com";          "first name" = Cali;          "last name" = Wright;          "mailing address" = "667 Fake Street";          "mailing address 2" = "Apt 4102";          nid = 125;          "node_title" = "Cali Wright";          notes = "test notes";          phone = "778-867-7184";          scheduleddate = none;          scheduledtime = none;          "state or province" = BC;          "zip code" = none;      }  )  2021-02-10 16:53:34.914652-0800 -[1784:532468] THE FINAL FILTER (  )  2021-02-10 16:53:34.914868-0800 -[1784:532468] THE FINAL FILTER (  )  

The crash that occurs is:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray'  

And lastly, in the code, it seems to crash right before this line:

 NSString *photo = [self.finalfiltered valueForKey:@"client photo"][indexPath.row];  
https://stackoverflow.com/questions/66145947/obj-c-app-crashing-if-tableview-section-is-empty February 11, 2021 at 06:19AM

没有评论:

发表评论