-1

私はiPhone開発の初心者です。セル内にラベル付きのテーブルビューを含むアプリを開発しています。if(indexpath.row == 0)セルの行ごとに、このように行ごとに異なるラベルを付けました。しかし、テーブルビューをスクロールしていると、ラベルが混ざり合っています。私を助けてください!

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

if (indexPath.row == 2)
{
    pendingListNumberLabel = [[UILabel alloc] init];
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
    pendingListNumberLabel.textColor = [UIColor orangeColor];
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:pendingListNumberLabel];
}

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
4

6 に答える 6

0

これは、再利用可能なセルを使用していて、正しい方法ではないために発生しています。

コードを置き換えるだけです-

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

これとともに -

UITableViewCell *cell = [[UITableViewCell alloc] init];

上記のコードで問題を解決できますが、セルの数が多い場合は適切ではありません。その場合、再利用可能なセルを使用する必要があります。

于 2012-05-23T06:37:56.083 に答える
0

tableView:cellForRowAtIndexPath:メソッドでは、最初に次のように選択した行を取得します

NSUInteger row = [indexPath row];

次に、「row」を使用NSArrayして、tableView行に対応するからデータを取得し、次のようにセルのtextLabelのtextプロパティに割り当てます。

NSString *string = [NSString stringWithFormat:@"%@",self.allItems objectAtIndex:row];
cell.textLabel.text = string;
return cell;

そしてもちろん、あなたはすでにUITableViewCellこのような再利用識別子でを作成しているでしょう:

static NSString *TableIdentifier = @"TableIdentifier";

UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleValue1
            reuseIdentifier:TableIdentifier];
}

重要なのは、テーブルの行に正確に対応する配列を使用することです。(つまり、配列の項目0はtableViewの行0に対応します)それなら間違いはありません。

于 2012-05-23T06:41:36.570 に答える
0
if (cell == nil)
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier] autorelease];
}
else
        {
            UILabel *titleLbl = (UILabel *)[cell viewWithTag:1];
            [titleLbl removeFromSuperview];
        }

if (indexPath.row == 2)
{
    pendingListNumberLabel = [[UILabel alloc] init];
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
    pendingListNumberLabel.textColor = [UIColor orangeColor];
    pendingListNumberLabel.tag = 1;// Write here.....................................
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:pendingListNumberLabel];
}

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;

お役に立てればと思います。

于 2012-05-23T06:50:24.693 に答える
0

UILabelをサブビューとして追加していますが、これは間違いです。通常、この種のものにはカスタムUITableViewCellを使用しますが、簡単な修正は次のとおりです。

if (indexPath.row == 2)
{
    pendingListNumberLabel = [[UILabel alloc] init];
    pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
    pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;
    pendingListNumberLabel.textColor = [UIColor orangeColor];
    pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);
    [pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
    pendingListNumberLabel.tag = 1337
    [cell addSubview:pendingListNumberLabel];
} else {
    for (UIView * subView in [cell subviews]) {
        if ([subView tag] == 1337) {
            [subView removeFromSuperview];
        }
    }
}
于 2012-05-23T07:01:42.123 に答える
0

if (cell == nil)チェックを省略するだけで、ラベルの混合の問題を修正できます。次のコードを使用します。

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) // Need not this checking 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

上記のようにセル割り当てを直接使用します:)

于 2012-05-23T07:31:52.583 に答える
-1

この問題を解決するために、条件cell=nil;をチェックする前に置くcell== nilか、毎回セルを作成することができます。それは動作しますが、それは良い習慣ではありません

これはテキストでも機能します

static NSString * CellIdentifier = @ "Cell";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

セル=nil;

if(cell == nil)

{{

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

if(indexPath.row == 2){

pendingListNumberLabel = [[UILabel alloc] init];

pendingListNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14];

pendingListNumberLabel.textAlignment = UITextAlignmentLeft ;

pendingListNumberLabel.textColor = [UIColor orangeColor];

pendingListNumberLabel.frame = CGRectMake(240, 6, 110, 30);

[pendingListNumberLabel setBackgroundColor:[UIColor clearColor]];
[cell addSubview:pendingListNumberLabel];

}

cell.textLabel.text = [affiliationsArray objectAtIndex:indexPath.row];

[セルsetAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

セルを返します。

于 2012-05-23T06:34:42.420 に答える