如何每2分钟重新加载UITablViewCell?

时间:2015-10-23 11:45:47

标签: ios objective-c uitableview

我有一个NSArray,它将每2分钟从服务器更新一次。我需要将这些更新的NSArray值显示在UITableViewCell中。我尝试了以下代码,但它无效。

请告诉我需要更改哪个部分才能自动更新uitableview单元格。

-(void)viewDidAppear:(BOOL)animated{
    sharedManager=[Mymanager sharedManager];
    priceDict=[[NSDictionary alloc]init];
    NSTimer* myTime1r = [NSTimer scheduledTimerWithTimeInterval: 3 target: self
                                                      selector: @selector(getPrices) userInfo: nil repeats: NO];

    NSTimer* myTimer = [NSTimer scheduledTimerWithTimeInterval: 140 target: self
                                                      selector: @selector(callAfterSixtySecond:) userInfo: nil repeats: YES];
}

-(void)getPrices{
    priceDict=[sharedManager.priceArray copy];
    currentPrice =[priceDict valueForKey:[currency objectAtIndex:0]];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
         [[self tableView]reloadData];
     }];
}

-(void) callAfterSixtySecond:(NSTimer*) t
{
    appDel = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDel getPrice];
    [self.tableView beginUpdates];
    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        priceDict=[sharedManager.priceArray copy];
        currentPrice =[priceDict valueForKey:[currency objectAtIndex:0]];

        [[self tableView]reloadData];

        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:currentPrice]
                             withRowAnimation:UITableViewRowAnimationNone];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    if(section==0){
      return [preciousMetal count];
    } else{
      return [country count];
   }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 61;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *sectionName;
    switch (section)
    {
        case 0:
            sectionName = NSLocalizedString(@"Precious Metal", @"Precious metal");
            break;
        case 1:
            sectionName = NSLocalizedString(@"Currency", @"Currency");
            break;
            // ...
        default:
            sectionName = @"";
            break;
    }
    return sectionName;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
    tempView.backgroundColor=[UIColor darkGrayColor];

    UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
    tempLabel.backgroundColor=[UIColor clearColor];
    tempLabel.textColor = [UIColor whiteColor]; //here you can change the text color of header.
    tempLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
    tempLabel.font = [UIFont boldSystemFontOfSize:15];
    if(section==0){
    tempLabel.text=@"Precious Metal";
    }else{
        tempLabel.text=@"Currency";

    }

    [tempView addSubview:tempLabel];

    return tempView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section==0){
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.metalName.text= [[preciousMetal objectAtIndex:indexPath.row] capitalizedString];
        NSString *str=[NSString stringWithFormat:@"%@",[currentPrice valueForKey:[preciousMetal objectAtIndex:indexPath.row]]];
        cell.price.text=[str substringToIndex:5];
        return cell;
    }else{
        cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell1.coutry.text=[country objectAtIndex:indexPath.row];
        return cell1;
    }
}

2 个答案:

答案 0 :(得分:1)

您在那里使用了[self.tableView beginUpdates]。它包含对表视图的所有更改,直到您同时调用[self.tableView endUpdates]来应用它们。

我认为它应该像这样工作,甚至可能没有NSOperationQueue的东西:

[appDel getPrice];
[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
    priceDict=[sharedManager.priceArray copy];
    currentPrice =[priceDict valueForKey:[currency objectAtIndex:0]];

    [[self tableView]reloadData];
}];

答案 1 :(得分:0)

您可以使用NSTimer来执行此操作。

NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 120.0
                      target: self
                      selector:@selector(reloadTabe:)
                      userInfo: nil repeats:YES];