代码之家  ›  专栏  ›  技术社区  ›  Wolfgang Schreurs

使用nsmutableArray显示动态内容的uiTableView

  •  0
  • Wolfgang Schreurs  · 技术社区  · 15 年前

    很久以来一直在寻找解决方案,但还是没有成功,也许这里有人能帮我。

    我创建了一个应用程序,它运行一个线程,在后台与Web服务器通信。有时线程会接收我希望在UITableView中显示的信息。由于内容的动态特性,我决定NSmutableArray应该足够存储来自Web服务器的信息。

    每当我从WebService接收信息时,我的AppDelegate就会发送通知。我的uitableview注册接收我打算在uitableview中显示的某些类型的信息。

    UITableViewController的initWithStyle方法包含以下代码:

    - (void)addContact:(NSNotification *)note {
        NSLog(@"%@", [note object]);
    
        NSString *message = (NSString *)[note object];
        NSString *name = [[message componentsSeparatedByString:@":"] objectAtIndex:2];
    
        contact = name;
        [array addObject:contact];
    }   
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        array = [[NSMutableArray alloc] initWithObjects:@"test1", @"test2", nil];
    
        tv.dataSource = self;
        tv.delegate = self;
    
        self.tableView = tv;
    }
    

    addcontact中的数组包含TableView中显示的数据。我在viewdidLoad:中手动添加到数组中的所有数据都将显示在表中,但在addContact:方法中添加的数据不会出现这种情况。

    有人知道我做错了什么吗?

    1 回复  |  直到 15 年前
        1
  •  1
  •   user169202    15 年前

    更新是否发生在主线程上? 您可以使用这种刷新函数来确保它是正确的。

    - (void)refresh {
    if([NSThread isMainThread])
    {
        [self.tableView reloadData];
        [self.tableView setNeedsLayout];
        [self.tableView setNeedsDisplay];
    }
    else 
    {
        [self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:YES];
    }
    

    }