-2

我有一个 UITableViewController,它使用 AFNetworking 从数据源加载单元格。数据加载异步,因此我设置了一个通知以在加载新数据时更新我的​​ tableView。每次新数据到达并更新我的对象属性时都会调用 reloadTable,但这些属性不会加载到我的 cellForRowAtIndexPath 中。

当我在重新加载表中 NSLog 我的属性时,它们是正确的。但是在我的 cellForRowAtIndexPath 中说 null

这是我的代码...

这将创建一个对象“shifts”并开始加载数据,然后在加载新数据并更新表时收到通知

NSDictionary *params = @{@"game_id":self.gameID, @"player_id":_sharedPlayer.playerID, @"category_id":self.categoryID};

 _shifts = [[ClipShiftController alloc] init];
[_shifts fetchShifts:params];

[[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(reloadTable:)
                                              name:@"clipLoaded"
                                            object:nil];

这是我的重载表:

- (void)reloadTable:(NSNotification *)notif
{
    [self.tableView reloadData];
    Clip *clip = [self.shifts.shiftDataSet objectAtIndex:i];
    NSLog(@"clip counter %i",[self.shifts.shiftDataSet count]);
    NSLog(@"clip name %@",clip.name);
    i++;
}

这是 cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    Clip *clip = [_shifts.shiftDataSet objectAtIndex:indexPath.row];
    NSLog(@"CELL COUNTER %i",[_shifts.shiftDataSet count]);
    NSLog(@"CLIP CELL name %@",clip.name);
    NSLog(@"row called");

    cell.textLabel.text = @"hello";
    //This is null  cell.textLabel.text = clip.name;

    return cell;
}

这是我的控制台输出

2013-08-26 12:16:50.831 One Six[15632:c07] clip counter 1
2013-08-26 12:16:50.831 One Six[15632:c07] clip name shift
2013-08-26 12:16:50.831 One Six[15632:c07] clip counter 2
2013-08-26 12:16:50.832 One Six[15632:c07] clip name shift
2013-08-26 12:16:50.832 One Six[15632:c07] clip counter 3
2013-08-26 12:16:50.832 One Six[15632:c07] clip name shift
2013-08-26 12:16:50.832 One Six[15632:c07] clip counter 4
2013-08-26 12:16:50.833 One Six[15632:c07] clip name shift
2013-08-26 12:16:50.833 One Six[15632:c07] clip counter 5
2013-08-26 12:16:50.833 One Six[15632:c07] clip name face off
2013-08-26 12:16:50.833 One Six[15632:c07] clip counter 6
2013-08-26 12:16:50.833 One Six[15632:c07] clip name face off
2013-08-26 12:16:50.834 One Six[15632:c07] clip counter 7
2013-08-26 12:16:50.834 One Six[15632:c07] clip name face off
2013-08-26 12:16:50.834 One Six[15632:c07] clip counter 8
2013-08-26 12:16:50.835 One Six[15632:c07] clip name shift
2013-08-26 12:16:50.835 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.835 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.836 One Six[15632:c07] row called
2013-08-26 12:16:50.836 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.836 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.837 One Six[15632:c07] row called
2013-08-26 12:16:50.838 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.838 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.838 One Six[15632:c07] row called
2013-08-26 12:16:50.839 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.839 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.839 One Six[15632:c07] row called
2013-08-26 12:16:50.840 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.840 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.841 One Six[15632:c07] row called
2013-08-26 12:16:50.841 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.841 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.842 One Six[15632:c07] row called
2013-08-26 12:16:50.842 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.844 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.844 One Six[15632:c07] row called
2013-08-26 12:16:50.845 One Six[15632:c07] CELL COUNTER 8
2013-08-26 12:16:50.845 One Six[15632:c07] CLIP CELL name (null)
2013-08-26 12:16:50.846 One Six[15632:c07] row called

显示的是我的表格视图中的 8 行打招呼。

4

3 回答 3

1

确保在模型对象中使用强而不是弱来声明属性。在没有看到您的代码的情况下,我猜这是最有可能的问题。在某些情况下,您确实想使用weak(例如:视图控制器中的IBOutlets),但在您拥有拥有其数据的数据对象的情况下则不然。

于 2013-08-28T03:37:50.857 回答
-1

你永远不会分配和初始化 var 'cell',检查你的代码,var 'cell' 仍然是 nul。

于 2013-08-26T21:07:56.860 回答
-2

我认为是因为您将 ivar (_shifts) 和属性 (self.shifts) 混合在一起。如果您使用旧版本的 Xcode,您必须使用以下代码 @synthesize 实现部分中的属性:

@synthesize shifts = _shifts;

如果您使用 Xcode 的最新版本,则无需声明 ivar _shifts 变量,只需使用以下代码声明属性:

@property (nonatomic, retain)  <YourObjectClass>* shifts;

在您使用 self.shifts 属性而不是 ivar 变量 _shifts 的代码的所有位置都需要

于 2013-08-26T19:14:06.317 回答