2010-07-23 27 views
0

嗨,我收到来自udp的2000数据,并在tableview中显示值。什么是最简单的方法来做到这一点?如何从NSDictionary显示UITableView单元格中的数据?

现在我使用两个nsthreads和一个线程通过udp接收数据并将其存储在NSMutableDictionary中。另一个线程使用这些字典值更新tableview。但它崩溃了我的应用程序。

下面是一些代码我用

予存储的接收值这样

NSMutableDictionary *dictItem 
CustomItem *item = [[CustomItem alloc]init]; 
item.SNo =[NSString stringWithFormat:@"%d",SNo]; 
item.Time=CurrentTime; 
[dictItem setObject:item forKey:[NSString stringWithFormat:@"%d",SNo]]; 
[item release]; 

委托方法我用和我用CustomTableCells显示数据作为列副。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [dictItem count]; 
} 



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *identifier = @"CustomTableCell"; 
    CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) 
    { 
     cell = [[[CustomTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
    } 
    NSArray *keys = [dictItem allKeys]; 
    CustomItem *item = [dictItem objectForKey:[keys objectAtIndex:indexPath.row]]; 
    cell.SNo.text = item.SNo; 
    cell.Time.text = item.Time; 
    return cell; 
} 

的errror是

终止应用程序由于未捕获的异常 'NSGenericException',原因: '***收集了同时列举了突变。' 2010-07-23 02:33:07.891 Centrak [2034:207]堆栈:( 42162256, 43320108, 42161198, 43372629, 41719877, 41719345, 9948, 3276988, 3237662, 3320232, 3288478, 71153942, 71153189, 71096786, 71096114, 71296742, 41650770, 41440069, 41437352, 51148957, 51149154,) 抛出'NSException'实例后终止调用

任何人都可以帮我吗?

在此先感谢.......

+0

没有办法来帮助您提供您提供的信息。也许如果你可以为你的表视图数据源委托方法发布一些代码,你从数据字典中提取数据并放入单元格。 – 2010-07-23 06:44:27

回答

0

你可能必须使用锁,因为当你从表视图访问您的字典,它或许与其它线程突变。 尝试查看NSLock文档。在突变你的字典之前做[myLock lock];和变异之后做[myLock unlock];。在其他线程中类似:在枚举字典之前,执行[myLock lock];并获取所有值之后再执行[myLock unlock];myLock是一个NSLock对象,必须在您的线程之间共享。

0

可变集合本质上不是线程安全的,所以如果您将它们与多个线程一起使用,则必须先创建一个不可变副本。例如,如果你想通过你的NSMutableDictionary所有钥匙,迭代,你可以这样做(假设你的NSMutableDictionary被称为mutableDictionary):

NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:mutableDictionary]; 

for(id key in dictionary) { 
    // Do anything you want to be thread-safe here. 
} 

如果你不想复制的字典,我想你可以使用锁或只是@synchronized指令如下:

@synchronized(mutableDictionary) { 
    // Do anything you want to be thread-safe here. 
} 

欲了解更多信息,请看看苹果的文件关于多线程和线程安全的对象:http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

相关问题