11

私は UITableView を使用していますが、スクロールするにつれてテーブルビューのセルが徐々に太くなっていることに気付きました。コンテンツを上書きしていて、これを止めたいのですが、どこが間違っているのかわかりません。

私のUITableViewでは、なんらかの理由でテーブルビューの内容をスクロールすると、手動で作成されたUILabelでめちゃくちゃになります。

後でカスタム セルが必要になるため、手動の UILabel が必要です。

上下にスクロールすると、ラベルがどんどん太くなっていきます。それらは常に重なり合い、下の行に影響を与えることさえあります (ビューポートに入る前であっても)。

それを続けると、セルの内容が理解できなくなります。

これは、backgroundColor が に設定されていない場合にのみ発生しclearColorます。

私は試みましたが、効果が[cellLabel setClearsContextBeforeDrawing:YES];あり[self.tableView setClearsContextBeforeDrawing:YES]; ませんでした。

私が使用するcell.textLabel.textと、問題は解決するようです。

コードと画像のサンプルは次のとおりです。

  // Simple table view
    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";

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

        // Configure the cell...
        //[self configureCell:cell atIndexPath:indexPath];


        NSString *txt = @"Product";


        //cell.textLabel.text = txt;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, cell.frame.size.height)];

        UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 35)];
        [cellLabel setText:txt];
        [cellLabel setFont:[UIFont boldSystemFontOfSize:12]];
        [cellLabel setBackgroundColor:[UIColor clearColor]];

        [cellView addSubview:cellLabel];
        [cellLabel release];
        [cell.contentView addSubview:cellView];
        [cellView release];


        return cell;
    }


Image follows;


![image of uitableview][1]


  [1]: http://i.stack.imgur.com/5lNy6.png


// Edit to include context

I am using a dictionary to display the contents of the UITableViewCells.

I have attempted to do the following;

    - (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        static NSString *CellIdentifier = @"Cell";

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

            [self configureCell:cell atIndexPath:indexPath];
        } // end if


        // Configure the cell...
        //
       // Moved to inside the cell==nil        

        return cell;
    }

-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{

    // Get the txt from the Dictionary/Plist... *removed due verboseness*

    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 35)];
    [cellLabel setText:txt];
    [cellLabel setFont:[UIFont boldSystemFontOfSize:12]];
    [cellLabel setBackgroundColor:[UIColor clearColor]];

    [cell.contentView addSubview:cellLabel];
    [cellLabel release];
}

これにより、上書きの問題は修正されますが、問題が発生します。ラベルが完全にランダムな場所に繰り返し表示されます。以下は単なる例であり、他のフィールドとラベルも繰り返されます。

下の図を参照してください。

uitableviewでラベルを繰り返す

4

7 に答える 7

17
    // cell reuse
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

既に使用されているセルが返されました。既に UILabel サブビューがあり、その上に別のサブビューを追加しています。追加のサブビューをセクションに配置します

   if (cell == nil) { //cell initialization

セルの初期化後に必要に応じてサブビューを編集します。たとえば、タグでアクセスできます。

于 2012-03-20T21:00:33.333 に答える
6

毎回同じ再利用セルにラベルを追加しているため、太くなっています。dequeueReusableCellWithIdentifier を使用すると、既に画面に表示されているセルを取得することになります。これは正しいことですが、既にラベルを付けています。ラベルは毎回セルに対して同じ位置にあり、同じ色などになるため (唯一の動的要素はテキストになります)、これらすべてを一度だけ設定する必要があります。

私の推奨する解決策は、必要なプロパティを持つカスタム セルを作成することです。したがって、この場合、作成します

@interfacce MyCustomCell : UITableViewCell
    @property (nonatomic) UILabel *cellLabel;
@end

プロパティ UILabel *cellLabel を指定し、MyCustomCell.m の init でラベル テキストを設定することを除いて、上記のすべてのコードを実行し、cell のインスタンスをすべて self に置き換えます。次に例を示します。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self)
    {
        self.cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 35)];
        [self.cellLabel setText:txt];
        [self.cellLabel setFont:[UIFont boldSystemFontOfSize:12]];
        [self.cellLabel setBackgroundColor:[UIColor clearColor]];
    }

    return self;
}

cellForRowAtIndexPath で MyCustomCell を使用し、セル == nil かどうかを確認します。セル ラベルも確認する必要がある場合があります。

if(cell == nil || cell.cellLabel == nil)

まったく同じ方法で初期化します。

cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

あとは、次のように設定するだけです。

cell.cellLabel.text = ....;

cellForRowAtIndexPath のコードは非常にクリーンで、メモリ効率が高く、バグが発生することはありません。

インターフェイス ビルダーでセルを MyCustomCell 型に設定することを忘れないでください。

于 2013-01-03T08:46:01.163 に答える
4

これは少し古いスレッドです。でも、誰かの役に立ち、

viewに追加されたものはcell、 で再利用される前に削除できますtableView

このコードはそれを行います、

for (UIView* view in [cell.contentView subviews])
{
    if ([view isKindOfClass:[UILabel class]])  //Condition if that view belongs to any specific class       
    {
        [view removeFromSuperview];
    }
}

これは、セルを構成する前に追加できます。

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];

} 

末尾のセルでの繰り返しを避けるために、セルのラベル値を nil にすることもできます。

cell.textLabel.text = nil;
cell.detailTextLabel.text = nil;
cell.textLabel.font = nil;
于 2014-06-25T12:01:58.857 に答える
4
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

           UITableViewCell *cell = (UITableViewCell*)[self.YourTableName dequeueReusableCellWithIdentifier:nil];        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        }


            return cell; 
        }

ReusablecellIdentifier nil を使用して、正しく機能するようにします.....

于 2012-08-13T11:57:42.543 に答える
0

UIView *cellView を UITableViewCell *cell の上に置かないようにしてください。UITableViewCell は UIView のサブクラスであるため、必要に応じてサブビューを追加できます。ただし、UITableViewCell には既にラベルが含まれています。

を使用するだけ[cell.textLabel setText:txt]です。

于 2012-03-20T21:02:05.987 に答える
-1

A-Live の回答が最善の解決策でした。

https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.htmlを見つけて、より幅広い例を挙げました。

ただし、私の実験では、上書きせず、セル値をランダムな位置に配置しない UITableViewCells を設定できました。

私が使用したコードは以下の通りです。

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

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 120, 35)];
    [cellLabel setFont:[UIFont boldSystemFontOfSize:12]];
    [cellLabel setBackgroundColor:[UIColor clearColor]];
    [cellLabel setTag:1];
    [cell.contentView addSubview:cellLabel];
    [cellLabel release];




    // TextInput setup    
    CGRect cellTextFrame = CGRectMake(200, 12, 65, 30);


    UITextField *txtInputField = [[UITextField alloc] initWithFrame:cellTextFrame];
    [txtInputField setTag:2];
    [txtInputField setDelegate:self];
    [txtInputField setClearButtonMode:UITextFieldViewModeWhileEditing];
    [txtInputField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
    [txtInputField setFont:[UIFont systemFontOfSize:12]];
    [txtInputField setReturnKeyType:UIReturnKeyDone];
    [txtInputField setTextAlignment:UITextAlignmentLeft];
    [txtInputField setKeyboardAppearance:UIKeyboardAppearanceDefault];
    [txtInputField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
    [txtInputField setAutocorrectionType:UITextAutocorrectionTypeNo];
    [txtInputField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    txtInputField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [txtInputField setBorderStyle:UITextBorderStyleRoundedRect];
    txtInputField.textColor = [UIColor colorWithRed:56.0f/255.0f green:84.0f/255.0f blue:135.0f/255.0f alpha:1.0f];
    //[txtInputField addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];     

    [cell.contentView addSubview:txtInputField];
    [txtInputField release];

} // end if


// Configure the cell...
//
//[self configureCell:cell atIndexPath:indexPath];


UILabel *label = (UILabel *)[cell viewWithTag:1];
[label setText:txt];

UITextField *txtField = (UITextField *) [cell viewWithTag:2];
[txtField setText:txtText];
[txtField setPlaceholder:txtPlaceholder];



return cell;
于 2012-03-21T08:21:50.450 に答える