简体   繁体   English

如何在NSDictionary中的UITableView单元中显示数据?

[英]How do I display data in a UITableView cell from an NSDictionary?

Hi i received 2000 of datas from udp and display the values in tableview .What is the easiest method to do this ? 嗨,我从udp接收了2000条数据,并在tableview中显示了这些值。最简单的方法是什么?

Now i am using Two nsthreads and one thread for receive data via udp and stores it in NSMutableDictionary.Another thread update the tableview using these Dictionary values. 现在我使用两个nsthreads和一个线程通过udp接收数据并将其存储在NSMutableDictionary中。另一个线程使用这些Dictionary值更新tableview。 But it crashes my app. 但这会使我的应用程序崩溃。

Here is some code i used 这是我使用的一些代码

I stored Received values like this 我存储了这样的接收值

 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];

Delegate method i used and i used CustomTableCells to display data as column vice. 我使用的委托方法以及我使用的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;
}

The errror is 错误是

Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection was mutated while being enumerated.' 由于未捕获的异常“ NSGenericException”而终止应用程序,原因:“ ***集合在枚举时发生了变异。” 2010-07-23 02:33:07.891 Centrak[2034:207] Stack: ( 42162256, 43320108, 42161198, 43372629, 41719877, 41719345, 9948, 3276988, 3237662, 3320232, 3288478, 71153942, 71153189, 71096786, 71096114, 71296742, 41650770, 41440069, 41437352, 51148957, 51149154, 2925426 ) terminate called after throwing an instance of 'NSException' 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,2925426)在引发'NSException'实例后终止调用

Can anyone help me ? 谁能帮我 ?

Thanks in advance....... 提前致谢.......

You probably have to use locking because when you access your dictionary from table view, it perhaps being mutated with other thread. 您可能必须使用锁定,因为从表视图访问字典时,它可能会与其他线程发生变异。 Try to look at NSLock docs. 尝试查看NSLock文档。 Before mutating your dictionary do [myLock lock]; 在对字典进行变异之前,请执行[myLock lock]; and after mutating do [myLock unlock]; 并在[myLock unlock];后执行[myLock unlock]; . Similar in other thread: before enumerating dictionary do [myLock lock]; 在其他线程中类似:在枚举字典之前,请先执行[myLock lock]; and after getting all values do [myLock unlock]; 在获取所有值之后,请执行[myLock unlock]; . myLock is a NSLock object and must be shared between your threads. myLock是一个NSLock对象,必须在线程之间共享。

Mutable collections are not thread-safe by nature, so if you use them with multiple threads, you have to create an immutable copy first. 可变集合本质上不是线程安全的,因此,如果将它们与多个线程一起使用,则必须首先创建一个不变的副本。 For example, if you want to iterate through all the keys in your NSMutableDictionary, you would do this (assuming your NSMutableDictionary is called mutableDictionary ): 例如,如果您要遍历NSMutableDictionary中的所有键,则可以这样做(假设NSMutableDictionary称为mutableDictionary ):

NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:mutableDictionary];

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

If you don't want to copy the dictionary, I suppose you could use locks or simply the @synchronized directive as follow: 如果您不想复制字典,我想您可以使用锁或仅使用@synchronized指令,如下所示:

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

For more information, please have a look at the Apple documentation regarding multithreading and thread-safe objects: http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html 有关更多信息,请查看有关多线程和线程安全对象的Apple文档: http : //developer.apple.com/mac/library/documentation/cocoa/conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM