简体   繁体   中英

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 ?

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. 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.

- (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.' 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'

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. Before mutating your dictionary do [myLock lock]; and after mutating do [myLock unlock]; . Similar in other thread: before enumerating dictionary do [myLock lock]; and after getting all values do [myLock unlock]; . myLock is a NSLock object and must be shared between your threads.

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 ):

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(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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