2

私の問題は次のとおりです。変更可能な配列をに割り当てて、cellForRowAtIndexPath各配列オブジェクトをセルに表示します。これまでのところ、セルは期待どおりに表示されています。今、私は最初のセルに(条件に従って)aを表示したいUILabelので、他の可変配列オブジェクトは2番目のセル、3番目などにシフトされます。問題は、その条件をテストすると、 true の場合、は最初のオブジェクトとともにUILabel最初のセルに表示されます。実際には、2 つの要素が同じセルにありますが、これは私が期待するものではありません。(条件が true の場合) すべての要素をシフトして、最初のセルを.UIlabel

私が期待するものを私に与えない私の関連コードはこれです:

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


     UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"any-cell"];


 // Add and display the Cell     
      cell.tag = [indexPath row];
      NSLog(@"cell.tag= %i",cell.tag);
      //test the condition, if it's ok, then add the label to the first cell
      if ([self isNoScoreLabelDisplayed] && cell.tag==0) {
        UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 50)];
        [lbl setBackgroundColor:[UIColor greenColor]];
        [cell addSubview:lbl];
      }
    cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(


      //
      if (indexPath.row < cellList.count) {

            [cell addSubview:[cellList objectAtIndex:[indexPath row]]];//cellList is the mutable array from which i get all the elements to display in the cells

      }else{

            [cell addSubview:nextButton];
      }


      return cell;
}

私のロジックに何か欠けていますか?事前に感謝します。

4

4 に答える 4

1

「タイトル」セルに対してオフセットする必要がある場合、indexPath.row をモデル インデックスに直接関連付けています。

if (indexPath.row && (indexPath.row - 1) < cellList.count) {
            [cell addSubview:[cellList objectAtIndex:indexPath.row - 1]];
} else {
于 2012-07-13T07:42:40.907 に答える
1

これには 2 つのセル識別子を使用します。1 つは LabelCell 用で、もう 1 つは通常の ArrayCell 用です。これにより問題が解決され、ラベルとオブジェクトを含むセルは取得されません。

また、何をしているのかよくわかりませんが、毎回セルにサブビューを追加しているように見えますが、どこにも削除していません。セルが再利用されることを忘れないでください...

于 2012-07-13T07:55:19.643 に答える
1

こんにちは、最初の行の indexPath.row==0 < cell.count のため、ラベルの状態を確認してから同じオブジェクトを mutablearray に追加しているようです。

cell.tag = [self isNoScoreLabelDisplayed]?[indexPath row]+1:[indexPath row];//here i wanted to shift the tags in case the condition is true, so that all the elements will be displayed from the second cell. But seems not doing what i want :(

したがって、ラベルを表示する必要がある場合、上記のコードは単にセルタグを indexpath.row + 1 に設定しますが、以下のコードは (最初は indexPath.row == 0 であることを思い出して、ラベルを表示したとしても) 追加します。同じ配列オブジェクト:-)

if (indexPath.row < cellList.count) 
于 2012-07-13T08:00:15.170 に答える
0

ラベルのテキストをコンテナの最初の要素として挿入して確認できます。このようにして、インデックス オフセットの必要性や、コードのさらなる複雑化を回避できます。

例えば

[cellList insertObject:@"Label name" atIndex:0];
if ([cell tag] == 0) {
    // add required label
}
else {
    // do whatever you do for your standard cells, getting them with [cellList objectAtIndex:[indexPath row]];
}
于 2012-07-13T07:42:39.620 に答える