ios - 从 TableView 行向 NSMutableDictionary 添加值

标签 ios objective-c

我正在尝试构建一个函数来检查检索到的 JSON 值是否已更改(给定对话中的 messagecount)。我正在使用 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; 
}

以及我将值存储在 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 返回 2

嗯,我不确定我的计划是否走在正确的轨道上。所有的投入都受到高度赞赏。

最佳答案

我建议使用键值观察器来观察您的对象更改值。

您可以在这里阅读更多相关信息:

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

关于ios - 从 TableView 行向 NSMutableDictionary 添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19397731/

相关文章:

ios - 在 iOS 7.0.3 中使用 boundingRectWithSize 的 UILabel 渲染问题

ios - 无法为同一个 Firebase 项目中的第二个应用程序添加 Push Certificates

ios - 如何快速使用 Google Sign-In API

objective-c - 如何将自定义单元格添加到 UITableView 的底部?

ios - 从弹出窗口中的 UISwitch 操作隐藏 UIImageView (iPad)

ios - 如何在 Xcode 6 调试器中访问隐藏/缩写的堆栈帧(堆栈跟踪缩写 slider 去了哪里?)

objective-c - 在自动布局中,如何在删除自定义 View 后使 NSWindow 调整回最小尺寸?

Objective-C 将对象声明为与我声明的完全不同的类

ios - UILabel sizeThatFits 对于 1 行标签来说太宽了

ios - 如何将 UICollectionView 作为 subview 添加到 UIView?