【问题标题】:Add new row at first on TableView IOS首先在 TableView IOS 上添加新行
【发布时间】:2013-07-04 07:24:40
【问题描述】:

我想更新tableView 中的数据并将新数据作为UITableView 中的新初始行插入。 我有两个数组:FileName 数组包含我使用ParseXM 从服务器获取的数据和一个用于从FileNameArray 复制的临时数组。我写了一个函数UpdateArray来获取新数据,我将数据从FileNameArray复制到ViewDidLoad()中的临时数组,然后调用UpdateArray;首先我删除FileNameArray 中的所有条目,然后向调用ParseXML 的服务器发送请求,然后比较FileNameArray 和临时数组,如果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 不会更新。 你能告诉我解决方案吗?提前致谢。

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

【问题讨论】:

  • 请在此处输入一些代码以供您尝试?
  • 在[tableview reloadData]之前先更新数组
  • 到目前为止您尝试了什么,如果您尝试了,您的代码在哪里????
  • 我更新了我的代码。谢谢大家

标签: iphone ios uitableview object add


【解决方案1】:

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

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

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

这对你有用。

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 他希望新数据位于顶部,那么您如何使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 2021-03-26
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多