UiTableview值在更改数组值时未获得更新

时间:2014-08-22 04:37:36

标签: ios uitableview

我从一个返回数组的Web服务中获取数据,我在表视图中显示该数组。我面临的问题是表值正在更新,但在一段时间之后,它显示旧值,滚动时显示新值。

以下是我的代码。

NSURLRequest *request=[NSMutableURLRequest requestWithURL:url
                                              cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:8.0];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSMutableDictionary *ResultdicNew = [[NSMutableDictionary alloc]initWithDictionary:(NSMutableDictionary *)responseObject];

    NSString *message=[ResultdicNew objectForKey:@"status"];
    NSString *message1=@"";
    if([message isEqualToString:@"success"])
    {
        NSMutableDictionary *datadic=[ResultdicNew objectForKey:@"data"];
        NSMutableDictionary *listDataDic=[datadic objectForKey:@"competitors"];
        affArray=[listDataDic valueForKey:@"affiliation"];
        countArray=[listDataDic valueForKey:@"total_count"];
        _lblEventTitle.text=[datadic valueForKey:@"group_name"];

        NSLog(@"affArray here is %@",affArray);
        NSLog(@"countArray.count here is %ld",countArray.count);

        [self revealCompetitorsView];
    }
    else
    {
        message1=[Resultdic objectForKey:@"data"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:message1
                                                            message:message
                                                           delegate:self
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }

    [SVProgressHUD dismiss];

    // NSLog(@"JSON Here is :%lu",(unsigned long)Resultdic.count);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    [SVProgressHUD dismiss];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Data"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}];

[operation start];



#pragma mark - UITableView Datasource, Delegate Methods
#pragma mark -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [countArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    static NSString *CellIdentifier = @"menuCellNew";

    cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RAMenuCellNew" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        // [cell SetData:[m_ary objectAtIndex:indexPath.row]];
        UILabel *lbl=(UILabel*)[cell viewWithTag:420];
        UILabel *lbl1=(UILabel*)[cell viewWithTag:421];

        lbl.text=[affArray objectAtIndex:indexPath.row];
        lbl1.text=[countArray objectAtIndex:indexPath.row];
    }
    [self.sideTable reloadData];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSLog(@"%ld",(long)indexPath.row);
}

2 个答案:

答案 0 :(得分:1)

您的问题出在您的cellForRowAtIndexPath中 - 当您获得现有单元格以供重复使用时,您不会更新单元格。

您需要移动if (cell==nil)...块之外的单元格字段的更新 -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell;
    static NSString *CellIdentifier = @"menuCellNew";

    cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RAMenuCellNew" owner:self options:nil];
       cell = [nib objectAtIndex:0];
       cell.accessoryType = UITableViewCellAccessoryNone;
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
   }
// [cell SetData:[m_ary objectAtIndex:indexPath.row]];
   UILabel *lbl=(UILabel*)[cell viewWithTag:420];
   UILabel *lbl1=(UILabel*)[cell viewWithTag:421];

   lbl.text=[affArray objectAtIndex:indexPath.row];
   lbl1.text=[countArray objectAtIndex:indexPath.row];


   return cell;

}

此外,您可以在viewDidLoad中为您的单元格注册您的nib文件,然后您可以使用dequeueReusableCellWithIdentifer:forIndexPath,如果需要,它会自动分配新单元格,您可以跳过整个if (cell==nil...位。

您还应该考虑创建一个UITableViewCell子类,然后您可以将文本字段分配给IBOutlet属性,而不是按标签搜索。

答案 1 :(得分:0)

cellForRowAtIndexPath

上替换此代码
        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {

      UITableViewCell *cell;
     static NSString *CellIdentifier = @"menuCellNew";

    cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"RAMenuCellNew" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
        // [cell SetData:[m_ary objectAtIndex:indexPath.row]];
        UILabel *lbl=(UILabel*)[cell viewWithTag:420];
        UILabel *lbl1=(UILabel*)[cell viewWithTag:421];
        lbl.text=[affArray objectAtIndex:indexPath.row];
        lbl1.text=[countArray objectAtIndex:indexPath.row];

           return cell;
        }