2013-03-02 50 views
1

我以这种方式增加了两个自定义UILabel给我UITableView一节:的UITableViewCell和的UILabel

//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)我得到一个奇怪的效果:文本重叠只是让每个字母变粗。

怎么了?

回答

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


    } 
} 
+0

非常感谢! – SirSeymour 2013-03-03 21:41:42

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

    } 
}