-2

我想更新数据tableView并将新数据作为新的初始行插入到UITableView. 我有两个数组:FileName数组包含我使用从服务器获取的数据ParseXM和一个临时数组,用于从FileNameArray. 我写了一个函数UpdateArray来获取新数据,我将数据复制FileNameArray到临时数组中ViewDidLoad(),然后调用UpdateArray;首先我删除所有条目,然后FileNameArray向服务器发送一个请求ParseXML,然后我比较FileNameArray和临时数组,如果FileName arr > temp arr:删除所有条目并从arrtemp arr复制到,然后重新加载数据但不显示第一行中的新数据( s)。FileNametemp arrUITableviewtableView

cellForRowAtIndexPath():

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

    NSArray* FileNamereversed = [[FileCompletedArray reverseObjectEnumerator] allObjects];
    NSArray* UploadTimereversed = [[UploadTimeArray reverseObjectEnumerator] allObjects];
    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 %@",FileNamereversed);
        FileNameLabel.text =[FileNamereversed 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 = [UploadTimereversed 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];
    }
    //[temp removeAllObjects];
   // temp = [FileCompletedArray mutableCopy];
        return cell;
}

更新数组():

-(void)updateArray{
[NSThread sleepForTimeInterval:4.0];

        [FileCompletedArray 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); //000400010001
           // 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]) // compare 2 array
            {
                [temp removeallObjects];
                temp = [FileCompletedArray mutableCopy];
                [_tableView reloadData];

            }
            else
            {
               NSLog(@"2 array equal");
            }

}

当我打电话时UpdateArray,虽然服务器有新数据,但两个数组彼此相等,所以我tableView不会更新。你能告诉我解决方案吗?提前致谢。

编者的话:原来的措辞几乎是难以理解的——在更正时我可能遗漏或误读了某些方面。强烈建议原作者进行校对。

4

2 回答 2

0

要更新表格视图,您必须根据您的要求更新您的数组。

  1. 您可以将对象添加为:[yourArray insertObject:object atIndex:0]; //for first row.

  2. 然后用新数组重新加载表:[yourTable reloadData]

这对你有用。

于 2013-07-04T07:30:34.250 回答
0

@V-Xtreme 答案是正确的,另一个选择是......

而且对于做这种类型的工作,你需要使用insertRowsAtIndexPaths:withRowAnimation:方法,比如

[self.tableview insertRowsAtIndexPaths:indexPath withRowAnimation:YES ];

其中 indexPath 包含要添加的行号。

于 2013-07-04T07:31:21.810 回答