2013-03-02 79 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]; 

    } 
}