简体   繁体   English

从TableView行向NSMutableDictionary添加值

[英]Add values to NSMutableDictionary from TableView row

I'm trying to build a function that will check if a retrieved JSON value have changed ( messagecount in a given conversation). 我正在尝试构建一个函数,该函数将检查检索到的JSON值是否已更改(给定对话中的messagecount )。 I'm populating a TableView with my JSON data and I would like to store the value in a dictionary and compare them later when I do a data update. 我正在用我的JSON数据填充TableView,我想将值存储在字典中,并在以后进行数据更新时将它们进行比较。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ConversationsModel* conversation = _feed.conversations[indexPath.row];
static NSString *identifier = @"ConversationCell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];   
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:identifier];
}
[self getMessageCountToDictionary:conversation.messagecount id:conversation.conversationid];
cell.textLabel.text = conversation.title;  
return cell; 
}

And my method to store the values in a NSMutableDictionary: 我将值存储在NSMutableDictionary中的方法:

- (void)getMessageCountToDictionary:(NSNumber*)messagecount id:(NSString *)conversationid 
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
if (conversationid != NULL) {      
[dictionary setValue:conversationid forKey:[NSString stringWithFormat:@"%@", conversationid]];
[dictionary setValue:messagecount forKey:@"messageCount"];
dictionaryCopy =  [dictionary mutableCopy];
}
NSLog(@"Stored in dictionary %lu", (unsigned long)dictionary.count);
}

NSLog returns 2 NSLog返回2

Well, I'm not sure if I'm on the right track here for what I intend to do. 好吧,我不确定我是否打算这样做。 All inputs are highly appreciated. 所有输入均受到高度赞赏。

I would recommend to use key-value observer to watch your objects changing values. 我建议使用键值观察器来监视对象的值更改。

You can read more about it here: 你可以在这里读更多关于它的内容:

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

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

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