- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试用 selectedSegmentIndex 切换 cell.textLabel.text(两个不同的数组)。第一个 selectedSegmentIndex = 1
工作正常。
但是第二个崩溃了:
uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010ed70c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010e92abb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010ec5a093 -[__NSArrayM objectAtIndex:] + 227
3 ChillN 0x000000010be0363b -[EditFriends tableView:cellForRowAtIndexPath:] + 187
4 UIKit 0x000000010d2b99e8 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
5 UIKit 0x000000010d298208 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853
...
这是我的代码:查看加载:
- (void)viewDidLoad
{
[super viewDidLoad];
self.currentUser = [PFUser currentUser];
PFQuery *query = [PFUser query];
[query orderByAscending:@"name"];
// [query whereKey:@"objectId" notEqualTo:self.currentUser.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else {
self.allUsers = objects;
[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}
}];
self.tableData = [[NSMutableArray alloc] init];
[self getPersonOutOfAddressBook];
}
编号的行数:
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
switch (self.segment.selectedSegmentIndex)
{
case 0:
return [self.allUsers count];
break;
case 1:
return [self.tableData count];
break;
}
return 0;
}
cellforrowatindexpath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
// [accessoryView setImage:[UIImage imageNamed:@"circle_arrow_right.png"]];
// [cell setAccessoryView:accessoryView];
PFUser *selected = [self.allUsers objectAtIndex:indexPath.row];
if ([self isFriend:selected]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
Person *person = [self.tableData objectAtIndex:indexPath.row];
switch (self.segment.selectedSegmentIndex)
{
case 0:
cell.textLabel.text = [[self.allUsers objectAtIndex:indexPath.row] valueForKey:@"surname"];
break;
case 1:
cell.textLabel.text = person.fullName;
break;
}
return cell;
}
索引改变了:
-(IBAction) segmentedControlIndexChanged
{
if(self.segment.selectedSegmentIndex == 0)
{
self.tableView.hidden = NO;
}
else if (self.segment.selectedSegmentIndex == 1)
{
self.tableView.hidden = NO;
}
[self.tableView reloadData];
}
我该如何解决? :)
最佳答案
您的代码可能会在这些行上崩溃
PFUser *selected = [self.allUsers objectAtIndex:indexPath.row];
或 Person *person = [self.tableData objectAtIndex:indexPath.row];
这是因为您没有检查 selectedSegmentIndex 是否已更改。因此,如果 tableview 数据源将使用 self.allUsers,则 [self.tableData objectAtIndex:indexPath.row];
可能会崩溃,如果它将使用 self.tableData
,则 [ self.allUsers objectAtIndex:indexPath.row];
可能会崩溃
我已经修复了你的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
switch (self.segment.selectedSegmentIndex)
{
case 0:
PFUser *selected = [self.allUsers objectAtIndex:indexPath.row];
if ([self isFriend:selected]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [[self.allUsers objectAtIndex:indexPath.row] valueForKey:@"surname"];
break;
case 1:
Person *person = [self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = person.fullName;
break;
}
return cell;
}
关于ios - 具有 selectedSegmentIndex 的 cellForRowAtIndexPath 中的 NSRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31730064/
我正在编写一个基于核心数据的 iPhone 应用程序,而且我是 Objective-C 的新手。 我有一个正在努力解决的错误。 iPhone 模拟器不断崩溃并显示以下错误消息: 2010-03-21
当我尝试将属性应用于子字符串时,有时在以下代码中崩溃: // Dummy strings let originalString = "Some example string" let searchSu
我试图在按下主页按钮时保存数据。这是我的相关代码。 - (NSString *) saveFilePath { NSArray *path = NSSearchPathForDi
我有一个应用程序,用户报告该应用程序随机崩溃。所有崩溃都有相同的堆栈 0 CoreFoundation TAGPBIvarClearUInt32 (in SPONTAN) + 11 1 l
我在下面的方法中得到 substringWithRange:range 的异常。 我有禁用编辑的 TextView 。 我仅将文本字段用于文本选择。当我第一次选择文本时没有异常(exception),
Application Specific Information: *** Terminating app due to uncaught exception 'NSRangeException',
大家好,我是 swift 的新手,我的程序面临一个问题。我已在“let defaultStudentId = NSUserDefaults.standardUserDefaults()”中保存一些数据
我试图在从 JSON 解析对象后填充 TableView 。我想将行数设置为已解析对象的计数。 这是我的 viewDidLoad 代码: - (void)viewDidLoad { [supe
我的应用在 _storeArray 中随机崩溃。 在 viewDidLoad 中,有一个方法 [self loadUrl] 解析 xml 文件,然后添加字典 [_storeArray addObjec
我仍在边做边学,所以如果这听起来像是一个菜鸟问题,那可能就是这样。 我正在尝试遍历 NSDictionary(消息)并获取某个键的值。 当我运行这段代码时,出现以下错误“* 由于未捕获的异常‘NSRa
我有一个我无法解决的问题。我正在使用 Core Data 框架创建一个 iOS 应用程序。在我得到 NSRangeException 之前,一切都很顺利。这个异常是我在检查应用程序数据是否需要更新的函
你好我是 iPhone 编程的新手,我正在使用 EGOPhotViewer 并想通过使用此代码显示图像, for ( recipeImages in recipeImages.imgArray) {
我无法 repo 这个问题。我明白 NSRangeException 代表什么,但我不明白为什么它会在创建 nib 文件时出现。这是我从 Crashlytics 收到的崩溃报告: Fatal Exce
我尝试将 360 全景图加载到 UIViewController 的 UIWebView 中,该全景图采用 html 格式并内置于 krpano 中。当全景图打开时,一切正常。虽然过了一段时间后,我得
我正在尝试从相机捕捉图像和录制视频,但每当我用相机关闭 View 时,我都会收到此错误: Terminating app due to uncaught exception 'NSRangeExcep
应用程序在 iOS 11 更新之前运行良好。在 iOS 11 推出后,一些用户遇到了以下崩溃,但我无法在模拟器 iOS 11 中重现此问题。基于 fabric.io,并非所有 iOS 11 都经历过此
在向我的应用程序添加新的 Core Data 模型版本后,我执行了轻量级迁移,显然是成功的。迁移的文件加载正常,但在第一次尝试通过特定关系访问属性时,应用程序崩溃并显示 NSRangeExceptio
在我的应用程序中,更新表格内容后,我尝试将 UITableView 滚动到顶部一次。然而,在某些情况下,我的 table 是空的。所以我得到了以下异常: Terminating app due to
我有一个 NSRangeException 错误,该错误并不总是发生(尤其是在调试时)。它是随机出现的,我无法弄清楚它来自哪里。我有很多数组操作,因此很难以这种方式消除它。 我的问题是我是否可以从调试
我正在做教程并收到以下错误。 我的代码: #import int main (int argc, const char * argv[]) { //NSAutoreleasePool *p
我是一名优秀的程序员,十分优秀!