带有 selectedSegmentIndex 的 cellForRowAtIndexPath 中的 NSRangeException
NSRangeException in cellForRowAtIndexPath with selectedSegmentIndex
我尝试用 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;
}
我尝试用 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;
}