1

我以这种方式将两个自定义添加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];


    } 
}

一切都很好,但是当我上下滑动表格视图(试图覆盖下面的单元格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 回答