UITableViewCell和UILabel

Sir*_*our 1 uitableview uilabel ios

我正以这种方式UILabel向我的一部分添加两个自定义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];


    } 
}
Run Code Online (Sandbox Code Playgroud)

所有的东西都很完美,但是当我在桌面上下滑动(试图覆盖下面的细胞UINavigationBar)时,我得到一个奇怪的效果:文本重叠只是让每个字母都变粗.

怎么了?

B.S*_*.S. 6

cellForRowAtIndexPath每当细胞变得可见时调用方法.这就是每次滚动时都会创建标签的原因.解决方案是在创建单元格时创建Label:

 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];


   } 
}
Run Code Online (Sandbox Code Playgroud)