1.首先明确通知改变UI的方法类似于android中得handler方法
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
其中updateUI是进行改变UI控件的执行方法
2.获取数据
创建子线程不停的获取数据
//启动子线程来获取数据
NSThread* thread = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
[thread start];
其中run是线程执行的方法:
start是启动线程
//子线程需要执行的方法
- (void)run
{
dictionary =[[NSMutableDictionary alloc ] init];
while (true)
{
[NSThread sleepForTimeInterval:0.5];
[udpClient returnIPAndPcName:dictionary];
NSEnumerator* em = [dictionary keyEnumerator];
id key;
while(key=[em nextObject])
{
NSLog(@"%@",key);
[ip addObject:key];
NSLog(@"%@",[dictionary objectForKey:key]);
[name addObject:[dictionary objectForKey:key]];
}
[name addObject:@"请手动输入IP:"];
[ip addObject:@"192.168.1.XXX"];
if (ip.count>0)
{
break;
}
}
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES];
}
3.updataUI的方法是
-(void)updateUI
{
NSLog(@"自动刷新主机列表---");
[self.table reloadData];
}
参数说明:
其中[self.table reloadData];是UITableView的数据重新载入的方法,刚开始的时候还不知道,后面查资料终于知道了,UITableView的是初始化的时候载入数据的,所以动态刷新必须使用这个方法。