7

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

这是图像:

在此处输入图像描述

4

10 回答 10

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;
}
于 2013-08-30T15:07:56.163 回答
2

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

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

于 2013-08-28T04:13:44.780 回答
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;
}
于 2013-08-28T04:40:38.377 回答
2

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

for(UIView *view in cell.contentView.subviews){  
        if ([view isKindOfClass:[UIView class]]) {  
            [view removeFromSuperview];   
        }
    }
于 2014-09-22T08:52:13.480 回答
2

添加这一行:

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

前:

[cell.contentView addSubview:lblDate];
于 2016-02-04T18:57:34.817 回答
0

您编写从单元格子视图中删除所有内容的代码是正确的。但是你写错地方了。 UITableView'sdequeueReusableCellWithIdentifier将在分配和初始化一次后返回单元格。因此,您为删除而编写的代码cell.contentView.subViews 将永远不会运行,并且您会得到 Overlapped 视图。

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

 UILabel *lblDate = nil;
  if (cell == nil)
  {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    lblDate = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 215, 10)];
    lblDate.textColor = [UIColor grayColor];
    lblDate.font = [UIFont systemFontOfSize:10.0];
    lblDate.tag = 1;
    [lblDate setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:lblDate];
  }
  else
  {
    //get a reference to the label in the recycled view
    lblDate = (UILabel *)[cell.contentView viewWithTag:1];
  }
于 2013-08-28T05:22:01.770 回答
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;}
于 2013-08-28T07:18:15.767 回答
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;
}
于 2014-07-07T11:47:15.833 回答
0
if ([cell.contentView viewWithTag:tagnumber]
    {
        [[cell.contentView viewWithTag:tagnumber]removeFromSuperview];

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

这一行就足够了,只需删除以前的标签子视图并添加带有标签的新子视图 .. 感谢您的回答

于 2016-01-19T07:41:28.737 回答
0

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

于 2016-10-14T07:09:36.797 回答