NGOT
NGOT

Reputation: 149

Update Data and show it on UITableView iOS

I have a problem: update data in UITableView. I want to get new data from parseXML that response from Server and them update new data to UITableView. **I used beloww code, but it does not show new data on Table View. I wrote a UpdateArray() function to check new data and then I compare 2 Array,if diff [Array count] then I call [tableview reloadData];

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

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

    }

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



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

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];


            UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
            FileNameLabel.backgroundColor = [UIColor clearColor];
            FileNameLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
            FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
            FileNameLabel.textColor = [UIColor blackColor];
            NSLog(@"Reseversed TEMP array %@",temp);
            FileNameLabel.text =[temp objectAtIndex:indexPath.row];
            [cell.contentView addSubview: FileNameLabel];
            [FileNameLabel release];

            UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 25)];

            UploadTimeLabel.backgroundColor = [UIColor clearColor];
            UploadTimeLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            UploadTimeLabel.textColor = [UIColor grayColor];
            UploadTimeLabel.text = [UploadTimeArray objectAtIndex:indexPath.row];
            [cell.contentView addSubview: UploadTimeLabel];
            [UploadTimeLabel release];

            UILabel *CompleteLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 12, 170, 25)];
            CompleteLabel.backgroundColor = [UIColor clearColor];
            CompleteLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
            CompleteLabel.textColor = [UIColor darkGrayColor];
            CompleteLabel.text =@"Completed";
            CompleteLabel.textAlignment = NSTextAlignmentRight;
            [cell.contentView addSubview: CompleteLabel];
            [CompleteLabel release];
        }
            return cell;
    }

UpdateArray()

 -(void)updateArray{

   while (loop)
   {
        [NSThread sleepForTimeInterval:4.0];

        [FileCompletedArray removeAllObjects];
     // [temp removeAllObjects];
      ....
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];

        [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Success");
            NSString * parsexmlinput = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"Response in Loop CompleteView: %@", parsexmlinput); 
           // dispatch_async(dispatch_get_main_queue(), ^{
                [self parseXMLFile:parsexmlinput];

            NSLog(@"File Completed array: %@", FileCompletedArray);
            NSLog(@"File Temp out array: %@", temp);
            NSLog(@"File Completed count: %lu",(unsigned long)[ FileCompletedArray count]);
            NSLog(@"File Temp out count: %lu", (unsigned long)[temp count]);
            // NSLog(@"State: %@", state);

            if([FileCompletedArray count ] != [temp count])
            {
                [temp removeAllObjects];
                temp= [FileCompletedArray mutableCopy];
                [_tableView reloadData];
            }
            else
            {
               NSLog(@"2 array equal");
            }
   //});
        }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                              NSLog(@"error: %@", error);

                                          }
         ];
        [httpClient enqueueHTTPRequestOperation:operation];
   }
}

Can you help me? Thanks in advance.

Upvotes: 1

Views: 4320

Answers (3)

Vishal
Vishal

Reputation: 8256

If you refresh table view then try this line & check:

[[self mytableview] reloadData];

mytableview is obj of tableview you give here your tableview object.

Upvotes: 0

Islam Adel
Islam Adel

Reputation: 575

I dont See that you call reloadData.

EDIT:

You must check that

1-Temp is having objects i mean that [temp count] is not returning zero.

2-The if condition that checks the two arrays is triggered . I mean that reload data is called.

3-You can make a breakpoint after cell.contetview addsubview and check what the cell contain now?

Upvotes: 1

geo
geo

Reputation: 1791

before you call

[_tableView reloadRowsAtIndexPaths:[_tableView indexPathsForVisibleRows]
                                 withRowAnimation:UITableViewRowAnimationNone];

you have to call

[_tableView beginUpdates];
...
[_tableView endUpdates];

or call

[_tableView reloadData];

Upvotes: 0

Related Questions