2012-05-18 43 views
0

后我具有从一个JSON调用每5秒刷新加载值的表。每当表更改时,添加新单元格时,当前选定的单元格将成为重新加载的最后一个单元格上方的单元格。你可以在我的get_counter选择器中看到我正在尝试执行的操作。提前致谢!我还是很新的ObjC所以只是想学习:)保持UITableView的选择的值的数据重新加载

- (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]; 
} 

} 

回答

2

我可以完全关闭这一个,但这可能是你的问题。

NSIndexPath *ipath = [table indexPathForSelectedRow]; 

对我来说,这看起来像你得到一个指针选定的行。所以当你更新表时,ipath也会得到更新。

尝试:

NSIndexPath *ipath = [[table indexPathForSelectedRow] copy]; 

的另一件事是,如果你添加一个新行则行指标有改变。 试试这个:

- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell 

所以这将是:信息

NSIndexPath *ipath = [table indexPathForSelectedRow]; 
UITableViewCell *cell = [table cellForRowAtIndexPath:ipath]; 
[table reloadData]; 
[table selectRowAtIndexPath:[table indexPathForCell:cell] animated:NO scrollPosition:UITableViewScrollPositionNone]; 

很多,但希望东西在里面会有所帮助。

+0

谢谢Jaybit ...虽然没有工作。 :( 当我做一个NSAPI的IPATH,它会出现为空,所以它可能是它在不同的类,该行被选中或什么? –

+0

“indexPathForSelectedRow 返回一个索引路径标识的行和部分 - (NSIndexPath *)indexPathForSelectedRow 返回值 标识选定行的行和段索引的索引路径,如果索引路径无效,则返回nil。“来自UITableView CLass Reference – Jaybit

相关问题