0

UITableView を設定するために使用する Web サービスから配列オブジェクトを受け取りました。Web サービスから新しい配列を取得し、テーブルにデータを入力する配列を再定義してから tableView をリロードして、この配列から新しいオブジェクトを使用するメソッドを実装しようとしています。これは、作業を行う必要があるメソッドです。

WSCaller *caller = [[WSCaller alloc]init];

    arrayFromWS = [caller getArray];
    [self.table reloadData];

そしてそれはうまくいきません。何か案は?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

NSString *CellIdentifier = @"productsCellIdentifier";

ProductsCell *cell = nil;

cell = (ProductsCell *)[self.table dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) 
{
    NSArray *topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"ProductsCell" owner:nil options:nil];

    for (id currentObject in topLevelObjects) 
    {
        if ([currentObject isKindOfClass:[ProductsCell class]]) 
        {
            cell = (ProductsCell *)currentObject;
            break;
        }
    }
}

if (productsMutableArray) 
{
    cell.backgroundColor = [UIColor whiteColor];
    cell.productName.text = [[self.productsMutableArray objectAtIndex:indexPath.row]objectForKey:@"name"];

}

return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [productsMutableArray count];
}
4

1 に答える 1

0

データソースの実装が間違っています (または少なくともスニペットが間違っています)。

まず、arrayFromWS を保持していません。

WSCaller *caller = [[WSCaller alloc] init];
arrayFromWS = [caller getArray];
[self.table reloadData];

次に、tableView:numberOfRowsInSection: および tableView:cellForRowAtIndexPath: メソッドで、別の配列 (productsMutableArray) を使用しています。

これを修正するには、上記のコードを次のように変更することをお勧めします (productsMutableArray が strong/retain プロパティであると仮定すると:

WSCaller *caller = [[WSCaller alloc] init];
self.productsMutableArray = [NSMutableArray arrayWithArray:[caller getArray]];
[self.table reloadData];
于 2012-06-27T15:02:34.743 に答える