I am creating a custom UITableView menu control. Each time I select a specific row, I save this row index path, so the next time the user selects another row, people can find out their previous selected row. So I added this to cellForRowAtIndexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; cell.textLabel.text = self.left[indexPath.row][@"name"]; [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:[[[NSUserDefaults standardUserDefaults] objectForKey:kPreviousSelectedRow] integerValue] inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone]; cell.selectionStyle = UITableViewCellSelectionStyleGray; cell.textLabel.highlightedTextColor = [UIColor grayColor]; return cell; }
and when the user selects another row, save this row: [[[[NSUserDefaults standardUserDefaults] objectForKey: kPreviousSelectedRow], so the next time he sees his previous selected row.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:indexPath.row] forKey:kPreviousSelectedRow]; }
Failure log: index [[[[NSUserDefaults standardUserDefaults] objectForKey: kPreviousSelectedRow] integerValue], and count is numberOfRows. As you can see, this should not go beyond. I do not know where [0 ... 6] comes from.
2013-08-23 21:01:26.107 [17605:c07] index:10, count:14 2013-08-23 21:01:26.173[17605:c07] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 7 beyond bounds [0 .. 6]'
EDITED: And if I scroll through the table view slowly, it will not work; if I scroll through it quickly, it will work. what?
source share