在TableView IOS的第一个位置添加新行



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

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不会更新。你能给我看看解决方案吗?提前谢谢。

编者按:最初的措辞几乎难以理解——在纠正时,我可能遗漏或误读了某些方面。强烈鼓励原作者进行校对。

要更新表视图,必须根据需要更新数组。

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

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

这对你有用。

@V-Xtreme的答案是正确的,另一个选项是…

而且为了完成这类工作,你需要使用insertRowsAtIndexPaths:withRowAnimation:方法,比如

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

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

最新更新