Reputation: 11
I have a table that is loading values from a JSON call that refreshes every 5 seconds. Every time the table changes, and a new cell is added, the currently selected cell becomes the one above the last one on the reload. You can see in my get_counter selector where I am trying to do it. Thanks in advance! I am still very new at ObjC so just trying to learn :)
- (void)viewDidLoad {
[super viewDidLoad];
repeat_timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(check_new) userInfo:nil repeats:YES];
}
-(void)check_new{
NSString *ret;
ret=[MyWebFunction is_new_orders:appDelegate.user_id Order_number:[[response_array objectAtIndex:0] objectForKey:@"order_id"] Type:type];
if([ret isEqualToString:@"yes"]){
[indicator startAnimating];
[self performSelector:@selector(get_counter) withObject:nil afterDelay:0.0005];
}
}
-(void)get_counter{
jsonString = [[NSMutableString alloc] initWithData:[MyWebFunction get_orders:appDelegate.user_id Type:type Limit:limit] encoding:NSUTF8StringEncoding];;
response_array = [[jsonString JSONValue] retain];
[indicator stopAnimating];
//this is my attempt at retaining the value :)
NSIndexPath *ipath = [table indexPathForSelectedRow];
NSLog(@"iPath: %@",ipath);
[table reloadData];
[table selectRowAtIndexPath:ipath animated:NO scrollPosition:UITableViewScrollPositionNone];
if([response_array count]>0){
total=[[[response_array objectAtIndex:0] objectForKey:@"total_orders"] intValue];
checksArray=[[NSMutableArray alloc] init];
for(int i=0;i<response_array.count;i++){
[checksArray addObject:@"0"];
}
}
sel_item_id=[[NSMutableArray alloc] init];
all_ids=nil;
if(repeat_timer){
[repeat_timer invalidate];
repeat_timer=nil;
repeat_timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(check_new) userInfo:nil repeats:YES];
}
}
Upvotes: 0
Views: 1272
Reputation: 1864
I could be totally off on this one but this might be you problem.
NSIndexPath *ipath = [table indexPathForSelectedRow];
To me this looks like your getting a pointer the selected row. So when you update the table ipath gets updated also.
try:
NSIndexPath *ipath = [[table indexPathForSelectedRow] copy];
The other thing is, if your adding a new row then the row indices have changes. try this instead:
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell
so it would be:
NSIndexPath *ipath = [table indexPathForSelectedRow];
UITableViewCell *cell = [table cellForRowAtIndexPath:ipath];
[table reloadData];
[table selectRowAtIndexPath:[table indexPathForCell:cell] animated:NO scrollPosition:UITableViewScrollPositionNone];
Lot of information but hopefully something in there will help.
Upvotes: 2