当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - UITableView crashing on row selection

问题描述:

In my app I have a switch that determines how the tableView is to be populated. If one side is selected, it will display all items from an xml feed, whereas if the other side is selected, it will show only those that have been downloaded to the device. When first opening the app, it displays all items by default, and all rows can be selected with no problem, and when selecting to show the downloads, all can be selected without issue as well; however, when returning to "show all", if you select a table cell that is farther down, the app will crash. It will only crash if you select a row that is farther down that the number of items downloaded, so I suspect this has something to do with the numberOfRowsInSections call, but I cannot for the life of me seem to fix it!

Here is my code within that method:

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

if (showDownloadsSwitch.selectedSegmentIndex == 0){

rows = itemsToDisplay.count;

}

else if (showDownloadsSwitch.selectedSegmentIndex == 1){

NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,

NSUserDomainMask, YES);

NSString *path = [[paths objectAtIndex:0]

stringByAppendingPathComponent:@"downloads"];

NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path

error:&error];

rows = fileList.count;

}

return rows;

}

EDIT:

I was able to fix it after a little more inspection. Turns out I was checking for a file at [downloads objectAtIndex:indexPath.row] before ensuring the table was populated with downloads, rather than all items. Thanks to all for your help!

网友答案:

You probably shouldn't use a property to store rows. Make it a local variable. If you need to access the number of rows in the table outside the delegate method, run the logic again separately.

The fileList local variable, however, seems like it should be a property. You are referencing a simar array in the didSelectRowAtIndex method, right? Could there be a discrepancy there?

Update:

- (void)viewDidLoad
{

  self.itemsToDisplay      = ...
  self.fileList            = ...
  self.showDownloadsSwitch = ...

  [self.showDownloadsSwitch addTarget:self.tableView
                               action:@selector(reloadData)
                     forControlEvents:UIControlEventValueChanged];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section == 0 && self.showDownloadsSwitch.selectedSegmentIndex == 0)
    return self.itemsToDisplay.count;

  if (section == 1 && self.showDownloadsSwitch.selectedSegmentIndex == 1)
    return self.fileList.count;

  return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (indexPath.section == 0)
    [self.itemsToDisplay objectAtIndex:indexPath.row];
  else
    [self.fileList objectAtIndex:indexPath.row];
}
网友答案:

Try including this one.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] init];
    return view;
}

It will not allow to show further rows to be selected if second choice is selected.

网友答案:

I think your array is empty let try below scenario

NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
   error:&error];
if(fileList)
{
    rows = fileList.count;
}
else
{
 // handle your error
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: