【问题标题】:NSArray and NSDictionary add duplicate objectsNSArray 和 NSDictionary 添加重复对象
【发布时间】:2012-01-24 01:17:40
【问题描述】:

向 NSArray 或 NSDictionary 添加对象时,我的应用只显示最后一个条目的副本。

我获取数据并将其发送给观察者...

    [[NSNotificationCenter defaultCenter] postNotificationName:@"xmlFinishedLoading" object:self userInfo:[NSDictionary dictionaryWithObject:currentEventObject forKey:@"notifKey"]];

此代码在观察者收到通知时运行...

     -(void)populateCurrentEventsTableView:(NSDictionary *)events
    {

     NSDictionary *info = [events valueForKey:@"userInfo"];
     Event *evt = [info valueForKey:@"notifKey"];
     if (self.eventDictionary == nil) {
     self.eventDictionary = [[NSMutableDictionary alloc]init];

     }
    [self.eventList addObject:evt];
    NSString *key = [NSString stringWithFormat:@"%i",[eventList count] - 1];
    [self.eventDictionary setValue:evt forKey:key];

    NSLog(@"events description = %@",evt.eventDescription);
    [self.tableView reloadData];

    }

使用此代码重新加载的 TableView...

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:       (NSIndexPath *)indexPath
    {
static NSString *CellIdentifier = @"ActivitiesList";



UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
NSUInteger row = [indexPath row];

NSString *rowKey = [NSString stringWithFormat:@"%i",row];

Event *evt = [eventDictionary objectForKey:rowKey];

cell.textLabel.text = evt.eventDescription;
cell.detailTextLabel.text = evt.eventSecondaryDescription;
return cell;

}

显示事件描述的 NSLog 很好,它显示不同的条目。但是,如果我要显示 eventDictionary 的条目,它会显示相同的条目。有什么帮助吗?

【问题讨论】:

    标签: nsarray nsdictionary tableview


    【解决方案1】:

    更多包含代码会很有帮助,你所拥有的对我来说看起来不错。这段代码出现在什么方法中?每次您返回 userInfo 对象以填充它时,是否可能会删除 self.eventDictionary?

    【讨论】:

    • “它显示相同的条目”是什么意思? eventDictionary 是只有 1 项长还是正确的项目数(您尝试添加到其中的项目数),但 n 项中的每一项都相同?
    猜你喜欢
    • 2017-02-10
    • 2011-08-16
    • 2013-03-10
    • 1970-01-01
    • 2018-08-19
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多