0

変更前: http://tinypic.com/view.php?pic=2j6a4h4&s=6 変更 後: http://tinypic.com/view.php?pic=demxi&s=6

クリックした後の listView のセルのレイアウトに問題があります。前と後の写真でわかるように、セル内の 3 つのラベル (名前、本、章) がすべてめちゃくちゃになっていますか? 私のコードで何が欠けていましたか? /よろしく

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"BookmarkCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Bookmark *item = [self.items objectAtIndex:indexPath.row];

NSArray *chunks = [item.name componentsSeparatedByString: @","];

NSString *name;
NSString *book;
NSString *chapter;

if ([chunks count] > 0)
{
    name = [chunks objectAtIndex:0];
    if ([chunks count] > 1)
    {
        book = [chunks objectAtIndex:1];
        if ([chunks count] > 2)
        {
            chapter = [chunks objectAtIndex:2];
        }
    }
}

UIView * pNewContentView= [[UIView alloc] initWithFrame:cell.contentView.bounds];
CGRect labelFrame= pNewContentView.bounds;
labelFrame.size.height= labelFrame.size.height * 0.5;

UILabel* pLabel1=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel1];

labelFrame.origin.y= labelFrame.size.height;
UILabel* pLabel2=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel2];

labelFrame.origin.y= labelFrame.origin.y + labelFrame.size.height;
UILabel* pLabel3=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel3];

[cell.contentView addSubview:pNewContentView];

[pLabel1 setText:(name)];    
[pLabel2 setText:(book)];  
[pLabel3 setText:(chapter)];       




return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70; // height of tableView Cell
}
4

1 に答える 1

1

@onnowebが言ったことを拡張し、彼が正しいように回答セクションに移動するだけです:

何が起こっているかというと、セルを更新するたびに、各セルにさらに 3 つのラベルを追加することで、実際に悪いメモリ ホグが発生します。ペイントの問題は、新しいラベルが古い原因を覆い隠しているために発生します。デフォルトでは、すべてのラベルに白い BG があります。

新しく作成されたセルを初期化する場所にラベル作成コードを移動したいのですが、既存のセルに追加されたラベルにアクセスするエレガントな方法がないという問題があります。私の提案は、このようなカスタマイズされたセルでは、常に UITableViewCell のカスタマイズされたサブクラスを作成することです。(私は個人的にそれが最もエレガントなアプローチだと思います):

例: UIMyCustomTableViewCell.h

@interface UIMyCustomTableViewCell : UITableViewCell {

}
@property (nonatomic, retain) UILabel *label1;
@property (nonatomic, retain) UILabel *label2;
@property (nonatomic, retain) UILabel *label3;

@end

UIMyCustomTableViewCell.m

@implementation UIMyCustomTableViewCell
@synthesize label1 = _label1;
@synthesize label1 = _label2;
@synthesize label1 = _label3;

... init, memory cleanup, etc.

@end

その時点で、cellForRowAtIndexPathコードでそのセルを使用できます。

if (cell == nil)
{
    cell = [[UIMyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    CGRect labelFrame = cell.bounds;
    labelFrame.size.height= labelFrame.size.height * 0.5;

    cell.label1 = [[UILabel alloc] initWithFrame:labelFrame];
    etc...
}

cell.label1.text = ...
cell.label2.text = ...
cell.label3.text = ...

また、上記のようなコードでラベルを初期化するのではなく、xib ファイルでカスタマイズされたすべてのセルのルック アンド フィールを設定することも大好きです。

お役に立てれば。

于 2012-08-28T21:05:13.240 に答える