1

この方法UILabelで私のセクションに2つのカスタムを追加しています:UITableView

//in .h file:
NSArray *listaopzioni;
@property (nonatomic, retain) NSArray *listaopzioni;

//in .m file:
self.listaopzioni = [[NSArray arrayWithObjects:@"Strumenti",@"Help & Credits", nil] retain];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    if ([indexPath section]==0) {

        cell.accessoryType = UITableViewCellAccessoryNone;

        UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
        slogan.text=[listaopzioni objectAtIndex:indexPath.row];
        slogan.textAlignment=UITextAlignmentCenter;
        slogan.font= [UIFont boldSystemFontOfSize:20];
        slogan.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:slogan];
        [slogan release];


    } 
}

すべてが完璧に機能しますが、テーブルビューを上下にスライドすると ( の下のセルをカバーしようとしてUINavigationBar)、奇妙な効果が得られます。テキストが重なって各文字が太くなります。

どうしたの?

4

2 に答える 2

6

メソッドcellForRowAtIndexPathは、セルが表示されるたびに呼び出されます。そのため、スクロールするたびにラベルが作成されます。解決策は、セルを作成するときにラベルの作成を配置することです。

 if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    if ([indexPath section]==0) {

    cell.accessoryType = UITableViewCellAccessoryNone;

    UILabel *slogan= [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
    slogan.text=[listaopzioni objectAtIndex:indexPath.row];
    slogan.textAlignment=UITextAlignmentCenter;
    slogan.font= [UIFont boldSystemFontOfSize:20];
    slogan.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:slogan];
    [slogan release];


   } 
}
于 2013-03-02T21:26:48.270 に答える
1

UITableViewCells (適切に使用された場合) は再利用されます。つまり、作成された後、作成された状態、つまりセルにラベルが追加された状態が維持されます。あなたがする必要があるのは、このセルの再利用を有利に利用することです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel slogan;
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        slogan = [[UILabel alloc] initWithFrame:CGRectMake(0,0,cell.frame.size.width,cell.frame.size.height)];
        slogan.tag = 2121; // Any unique-to-the-cell, positive integer
        slogan.textAlignment=UITextAlignmentCenter;
        slogan.font= [UIFont boldSystemFontOfSize:20];
        slogan.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:slogan];
    } else {
        slogan = [cell viewWithTag:2121]; // Must match slogan.tag
    }

    if ([indexPath section]==0) {

        cell.accessoryType = UITableViewCellAccessoryNone;

        slogan.text=[listaopzioni objectAtIndex:indexPath.row];

    } 
}
于 2013-03-02T21:52:33.410 に答える