cellForRowAtIndexPath:滚动后UILabel重叠

时间:2013-08-28 04:02:24

标签: ios objective-c uitableview cell-formatting

cellForRowAtIndexPath:

cell.textLabel.text工作正常。

滚动后UILabel重叠。这是代码:

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

    if (cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        // I have tried it by removing views and without.  No difference.   
        NSArray *viewsToRemove = [self.tableView subviews];
        for (UITableView *table in viewsToRemove)
        {
            [table removeFromSuperview];
        }
    }


    NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
    NSString  *entityName= [[managedObject entity]name];
    cell.textLabel.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
    cell.textLabel.font=[UIFont systemFontOfSize:14.0];


    NSDate *date = [managedObject valueForKey:@"lastmoddate"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE, MMM d, YYYY  h:mm a"];
    NSString *dateString = [formatter stringFromDate:date];

    UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)];
    lblDate.text = dateString;
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:10.0];

    [lblDate setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:lblDate];
    return cell;
}

这是图片:

enter image description here

10 个答案:

答案 0 :(得分:6)

这就是我想出来的,它运作良好:

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

    NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
    NSString  *entityName= [[managedObject entity]name];

    NSDate *date = [managedObject valueForKey:@"lastmoddate"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE h:mm a MMM d, yy'''"];
    NSString *dateString = [formatter stringFromDate:date];

    UILabel *lblUser = [[UILabel alloc] initWithFrame:CGRectMake(30, 8, 215, 14)];
    lblUser.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
    lblUser.textColor = [UIColor blackColor];
    lblUser.font = [UIFont systemFontOfSize:16.0];
    lblUser.tag = 1;
    [lblUser setBackgroundColor:[UIColor clearColor]];

    UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(30, 21, 215, 20)];
    lblDate.text = dateString;
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:12.0];
    lblDate.tag = 2;
    [lblDate setBackgroundColor:[UIColor clearColor]];

    if ((([cell.contentView viewWithTag:1]) && ([cell.contentView viewWithTag:2])))
    {
        [[cell.contentView viewWithTag:1]removeFromSuperview];
        [[cell.contentView viewWithTag:2]removeFromSuperview];
    }

    [cell.contentView addSubview:lblDate];
    [cell.contentView addSubview:lblUser];


    return cell;
}

答案 1 :(得分:2)

dequeueReusableCellWithIdentifier:forIndexPath:保证返回一个单元格(一个新的或一个来自重用队列),所以你的if(cell == nil)子句永远不会被执行 - 这就是为什么它不会是否删除视图的区别。标签重叠,因为这是您设置它的方式。默认标签位于单元格的左侧,lblDate也位于左侧(距离左侧10个点)。即使你将lblDate向右移动,也可能无法显示,因为我认为默认标签是单元格的全宽。最好使用两个标签制作一个自定义单元格放置在您想要的位置。

在添加另一个标签之前,您还需要测试标签是否已存在。您可以为标签指定一个唯一标签,并检查带有该标签的视图,或者,我认为更简单的方法是在storyboard或xib中创建自定义单元格,并在那里添加标签。然后,您只需要在代码中将内容添加到它们中。

答案 2 :(得分:2)

试试这个

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

    if (cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)];

    [cell.contentView addSubview:lblDate];
    }


    NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
    NSString  *entityName= [[managedObject entity]name];
    cell.textLabel.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
    cell.textLabel.font=[UIFont systemFontOfSize:14.0];
lblDate.text = dateString;
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:10.0];

    [lblDate setBackgroundColor:[UIColor clearColor]];

    NSDate *date = [managedObject valueForKey:@"lastmoddate"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"EEE, MMM d, YYYY  h:mm a"];
    NSString *dateString = [formatter stringFromDate:date];
    return cell;
}

答案 3 :(得分:2)

这是重新创建单元格内容的问题。尝试使用以下代码段。

for(UIView *view in cell.contentView.subviews){  
        if ([view isKindOfClass:[UIView class]]) {  
            [view removeFromSuperview];   
        }
    }

答案 4 :(得分:2)

添加以下行:

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

在:

[cell.contentView addSubview:lblDate];

答案 5 :(得分:0)

您正在编写用于从单元格子视图中删除所有内容的代码。但你把它写在错误的地方。 UITableView的{​​{1}}将在分配并初始化一次后返回单元格。因此,您为删除dequeueReusableCellWithIdentifier而编写的代码将永远不会运行,并且您将获得重叠视图。

您可以在else语句中使用该代码,但我不喜欢这种方式。为什么每次cell.contentView.subViews需要单元格时分配和初始化所有contentView。相反,我会创建一次UITableView并给它一个标签以便以后访问它。像这样:

UILabel

答案 6 :(得分:0)

试试这段代码。你的问题将得到解决。

-(NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section     
{

    return [arrTableData count];

}


-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UILabel *lblName;

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];

    lblName = [[[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 20.0)] autorelease];
    lblName.tag = LBLNAME;
    lblName.numberOfLines=1;
    lblName.textAlignment=UITextAlignmentLeft;
    lblName.font = [UIFont systemFontOfSize:16.0];
    lblName.textColor = [UIColor blackColor];
    lblName.backgroundColor = [UIColor clearColor];
    lblName.autoresizingMask = UIViewAutoresizingFlexibleRightMargin  ;
    [cell.contentView addSubview:lblName];

}else{

    lblName = (UILabel *)[cell.contentView viewWithTag:LBLNAME];
}
if (arrTableData.count>0) { lblName.text=[NSString stringWithFormat:@"%@",[arrTableData objectAtIndex:indexPath.row]];

}

return cell;}

答案 7 :(得分:0)

试试这个

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

NSManagedObject *managedObject = [newClass objectAtIndex:indexPath.row];
NSString  *entityName= [[managedObject entity]name];

NSDate *date = [managedObject valueForKey:@"lastmoddate"];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"EEE h:mm a MMM d, yy'''"];
NSString *dateString = [formatter stringFromDate:date];

UILabel *lblUser = [[UILabel alloc] initWithFrame:CGRectMake(30, 8, 215, 14)];
lblUser.text = [NSString stringWithFormat:@"%@   %i", entityName, [indexPath row]];
lblUser.textColor = [UIColor blackColor];
lblUser.font = [UIFont systemFontOfSize:16.0];
lblUser.tag = 1;
[lblUser setBackgroundColor:[UIColor clearColor]];

UILabel *lblDate = [[UILabel alloc] initWithFrame:CGRectMake(30, 21, 215, 20)];
lblDate.text = dateString;
lblDate.textColor = [UIColor grayColor];
lblDate.font = [UIFont systemFontOfSize:12.0];
lblDate.tag = 2;
[lblDate setBackgroundColor:[UIColor clearColor]];



[cell.contentView addSubview:lblDate];
[cell.contentView addSubview:lblUser];


return cell;
}

答案 8 :(得分:0)

if ([cell.contentView viewWithTag:tagnumber]
    {
        [[cell.contentView viewWithTag:tagnumber]removeFromSuperview];

    }
lblDate.tag = tagnumber;
    [cell.contentView addSubview:lblDate];

此行已经足够删除之前的标记子视图并添加了带标记的新子视图..感谢您的回答

答案 9 :(得分:0)

在我的情况下,同样的问题发生了,在2个位置分配tableviewcell,一个在tableviewcell自定义类中分配,并且必须分配自定义视图控制器,在我更改了alllocation后,一切都将正常工作。