2016-03-24 12 views
1

私はカスタムセルでuitableviewを持っています。TableViewの各セルに同じデータセットが設定されています

cellForRowAtIndexPathメソッドでは、コアデータから値を取得して表示します。

データを一意に表示する必要がありますが、問題は、tableviewの各セルが同じデータセットを表示していることです。

後は

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

    UITableViewCell *cell = nil; 
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:nil]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 



    self.productAmountTextLabel = [[UILabel alloc]init]; 

    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; 

    self.productAmountTextLabel.text = [device valueForKey:@"amount"]; 
    self.productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10]; 


    tableView.tableFooterView = [UIView new]; 

    cell.layer.borderWidth = 1.0; 
    cell.layer.cornerRadius = 10; 
    cell.layer.borderColor = [UIColor blackColor].CGColor; 



    [cell.contentView addSubview:self.productAmountTextLabel]; 
    return cell; 
} 

量ラベルは、DBから最初の値のみを取得し、すべてのセルの最初の値を表示し、私のcellForRowAtIndexPath方法です。どうすればこれを整理できますか?

+0

ivarでラベルを参照しないでください。ローカル変数を使用するだけで、このマサドの中にフッターを作成しないでください – Andrea

+0

私のコードを参考にして詳細を教えてください。ありがとう –

+0

self.productAmountTextLabelの代わりにcell.textLabelを直接使用する – Arun

答えて

1

を宣言しながら、uは行の値を返すにミスをしたときに、この問題は、一般的に発生した代わりのUITableViewCellのあなたのクラスを使用してセクション

あなたのこのコードをチェックしてください

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.devices count]; 

} 

たぶんuが値を返しましたその逆の場合は

+0

yeah..iは道であなたに感謝 –

+0

をロギングすることで、それをデバッグする二つのセル –

+0

これは、一度、あまりにも私の私に直面した問題でした。.. 。 –

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

    UITableViewCell *cell = nil; 
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"any-cell"]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"any-cell"]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     cell.layer.borderWidth = 1.0; 
     cell.layer.cornerRadius = 10; 
     cell.layer.borderColor = [UIColor blackColor].CGColor; 

     UILabel* productAmountTextLabel = [[UILabel alloc]init]; 
     productAmountTextLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10]; 
     productAmountTextLabel.frame = CGRectMake(0, 0, 100, 30); // for example 
     productAmountTextLabel.tag = 10000; // any number 
     [cell.contentView addSubview:productAmountTextLabel]; 
    } 

    // tableView.tableFooterView = [UIView new]; 

    UILabel* lbl = (UILabel*)[cell.contentView viewWithTag: 10000]; 
    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; 
    lbl.text = [device valueForKey:@"amount"]; 
    return cell; 
} 

- (void) makeFooter { 
    myTableView.tableFooterView = [UIView new]; 
} 
+0

私は試しましたが、同じ問題が残っています –

+0

'NSLog(@"%@ "、[device valueForKey:@" amount "]);'を記録しようとしましたが、値 –

+0

あなたの配列が適切でないと思います。 – Arun

1

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

    UITableViewCell *cell = nil; 
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:@"Cell"]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; 

    cell.textLabel.text = [device valueForKey:@"amount"]; 
    cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:10]; 

    cell.layer.borderWidth = 1.0; 
    cell.layer.cornerRadius = 10; 
    cell.layer.borderColor = [UIColor blackColor].CGColor; 

    return cell; 
} 

問題は、あなたが使用しているグローバルUILabelであるかもしれないこのコードを試してみてください。ラベルにカスタマイズが必要な場合は、カスタムUITableViewCellクラスを作成し、ラベルを宣言します。セルに

+0

の値を確認してください。 –

+0

self.devices配列の値が正しいかどうかチェックできますか? – Arun

+0

self.devices配列の値は適切ですが、NSLog(@ "%@"、[device valueForKey:@ "amount"]); –

関連する問題