-1

私はこの機能を実行します:

- (void)insertRows {
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

そして、この関数を実行する必要があります

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

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

しかし、この関数は実行されていません... tableViewの実行方法を教えてください... 関数

- (IBAction)insertObject {
    [[MasterViewController alloc] insertRows];
}

- このコードは insertRows を実行し、ブレークポイントでこれをチェックします

- (IBAction)insertObject:(id)sender {
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0   inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    NSLog(@"log:insert-indexPath: %i",indexPath.row);
    NSDate *object = _objects[indexPath.row];
    NSLog(@"log:insert-object %@",object.description);
 }

-それで動作します、なぜですか?

4

1 に答える 1

2

このコードにはいくつかの問題があります。

• おそらくしたい、addObject:したく_objectsないinsertObject:atIndex:

[[MasterViewController alloc] insertRows];意味がありません。initどこかにある必要があります。

descriptionユーザーに何かを表示する手段としてメソッドを使用しないでください。 descriptionデバッグとロギングのみを目的としています。

• の呼び出しをトリガーするにはtableView:cellForRowAtIndexPath:、通常reload、テーブルを呼び出します。つまり、そのメソッドは、それ自体を更新しているときに、テーブル ビューによって呼び出されます。

テーブル ビュー プログラミング ガイドから始めることをお勧めします。

于 2013-01-10T19:04:26.913 に答える