UITableView单元格中的UILabel在iOS7中相互重叠

时间:2013-09-27 06:07:26

标签: iphone ios objective-c uitableview

iOS6没有这个问题,但我目前正在使用iOS7。我有一个UITableView,你可以在加载视图时看到8个单元格。每个填充了不同的名称和数组。如果向下滚动,接下来的两个单元格看起来很好,但是过去的所有单元格都会放在它上面;该文本是以前单元格中的内容。因此,第10个细胞将具有第一个细胞中的细胞,以及应该放在其上的第10个细胞中的细胞。

代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        if (cell == nil) 
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

        //Create Label for Name inside cell
        UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 5.0, 300.0, 30.0 )];
        [name setText:[self.entriesArray objectAtIndex:indexPath.row]];


        //Check to see if current person is a Parent or Child
        NSString *class = [self.database getCellDataWithMembership:[self.MembershipArray objectAtIndex:indexPath.row] andColIndex:4];

        if([class isEqualToString:@"CHILD"])
        {
            name.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f];
            name.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];                
        }

        [cell.contentView addSubview:name];
        return cell;
}

我对表格视图的技巧充其量只是临时搭建。我一直在阅读大量文档和研究解决方案,但无法提出解决方案。我觉得奇怪的是它适用于iOS6,但不适用于iOS7。

所以它从数组中获取一个人的名字,我想用这些名称填充单元格。我最初能够使用以下方法完成此任务:

cell.textLabel.text = [self.entriesArray objectAtIndex:indexPath.row];

if([class isEqualToString:@"CHILD"])
{
    cell.textLabel.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];

如果我使用它来代替第一个代码块中的“名称”UILabel操作,那么它会完美地显示名称而没有文本覆盖,但问题是文本颜色。如果它们被标记为CHILD,那么它们应该是绿色文本和粗体。但是,向下滚动后,每个人都不应该变成绿色。

很抱歉这个冗长的问题。我一直在努力解决这个问题,围绕着我的大脑,我似乎无法弄明白。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:1)

我正在向单元格动态添加UI对象。作为“caglar”笔记,添加然后删除是probs不是最好的做法。但是,我也是这样做的。每次显示时,我都会向单元格添加大量UI对象,这是首先删除所有子视图。然后,willDisplayCell委托将它们添加回来。显然,如果您只想删除某些视图,则必须标记视图并对删除更具选择性。但您可以使用以下内容删除所有内容。

-(void)tableView:(UITableView *)tableView
 willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {

    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
}

确保您在单元格的contentView中添加/删除标签。在iOS7中,如果你不小心的话,你也会删除它。

希望它可以帮助你。

答案 1 :(得分:0)

在您的代码中,只要调用cellForRowAtIndexPath:方法,标签就会添加到单元格中。我的意思是你多次添加标签。您可以删除之前添加的标签。

//If label was added with tag = 500, remove it
for (UIView *sv in cell.contentView.subviews)
{
    if (sv.tag == 500)
    {
        [sv removeFromSuperview];
    }
}

//Create Label for Name inside cell
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake( 7.0, 5.0, 300.0, 30.0 )];
[name setText:[self.entriesArray objectAtIndex:indexPath.row]];
name.tag = 500;

然而,这个解决方案不是一个好的解决方案。我认为创建自定义UITableViewCell是正确的做法。

答案 2 :(得分:0)

您可以尝试使用此方法,当cellnil时,您需要修改UILabel* name,然后使用标记{{1}设置name标签然后,您可以使用此方法name.tag = 1000访问此标签。

UILabel* name = (UILabel*)[cell.contentView viewWithTag:1000];

}

答案 3 :(得分:-1)

您可以使用ios6 / 7 delta设置uilabel尺寸检查器。首先通过更改值x,y,宽度,高度设置ios7的总视图,然后更改ios6 / 7 delta中的值以使其适用于ios6。希望你得到

答案 4 :(得分:-2)

您可以更改此代码。

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; -> UITableViewCell *cell = nil;
相关问题