UITableView using NSMutableArray to display dynamic content
Been looking for a solution for a long while now, but still no success, perhaps someone here can help me.
I created an application that runs a thread that communicates with a webserver in the background.开发者_开发知识库 Occasionally the thread receives information that I'd like to display in a UITableView. Because of the dynamic nature of the content I decided a NSMutableArray should be sufficient to store the information from the webserver.
Whenever I receive information from the webservice my AppDelegate sends a notification. My UITableView registers to receive certain kinds of information which I intend to show in the UITableView.
The initWithStyle method of the UITableViewController contains the following code:
- (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;
}
The array in AddContact contains the data displayed in the TableView. All data I manually add to the array in ViewDidLoad: will be shown in the table, but it doesn't happen for the data added in the AddContact: method.
Anyone got an idea what I'm doing wrong?
Is the update happening on the main thread? You can use this sort of refresh function to make sure it does.
- (void)refresh {
if([NSThread isMainThread])
{
[self.tableView reloadData];
[self.tableView setNeedsLayout];
[self.tableView setNeedsDisplay];
}
else
{
[self performSelectorOnMainThread:@selector(refresh) withObject:nil waitUntilDone:YES];
}
}
精彩评论