- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self getLoadingTableCellWithTableView:tableView];
RssItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RssItemCell"];
// If break in here, url fetch maybe error or row is null
RssItem *feed = [[self.manager.feeds objectForKey:self.manager.currentChannel] objectAtIndex:indexPath.row];
cell.title.text = feed.title;
cell.description.text = feed.description;
cell.date.text = feed.date;
// if any image enclosure at rss parse
[cell.enclosure setImage:[UIImage imageNamed:@"logo.png"]];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue,
^{
UIImage *image = [[UIImage alloc] initWithData:feed.enclosure];
[cell.enclosure setImage:image];
[cell setNeedsLayout];
});
return cell;
}
我尝试获取RSS的空行时遇到问题(我的网络类别中没有帖子)
我想显示警报并自动转到主页。
卡在这里。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[self getLoadingTableCellWithTableView:tableView];
RssItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RssItemCell"];
@try {
NSLog(@"Trying to tetch");
// If break in here, url fetch maybe error or empty row
RssItem *feed = [[self.manager.feeds objectForKey:self.manager.currentChannel] objectAtIndex:indexPath.row];
cell.title.text = feed.title;
cell.description.text = feed.description;
[cell.enclosure setImage:[UIImage imageNamed:@"logo.png"]];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue,
^{
UIImage *image = [[UIImage alloc] initWithData:feed.enclosure];
[cell.enclosure setImage:image];
[cell setNeedsLayout];
});
return cell;
}
@catch (NSException *e) {
NSLog(@"catching %@ reason %@", [e name], [e reason]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@",e] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
TableViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
[self presentViewController:VC animated:YES completion:nil];
}
@finally {
NSLog(@"finally");
}
}
警报未出现,并转至断点。我应该如何处理此错误?
最佳答案
首先,您必须始终在cellForRowAtIndexPath:
返回UITableViewCell *。其次,这通常是一种不好的做法(请参见下面的引用),尤其是因为您可能会显示一些警报并多次显示TableViewController
。作为一个问题解决方案,似乎没有异常(exception),这使我认为self.manager.feeds
实际上是nil
。
Programming with Objective-C
You should not use a try-catch block in place of standard programming checks for Objective-C methods. In the case of an NSArray, for example, you should always check the array’s count to determine the number of items before trying to access an object at a given index. The objectAtIndex: method throws an exception if you make an out-of-bounds request so that you can find the bug in your code early in the development cycle—you should avoid throwing exceptions in an app that you ship to users.
For more information on exceptions in Objective-C applications, see Exception Programming Topics.
关于ios - 数组为空时的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17144227/