【问题标题】:When Scroll UITableView it overlap the name iOS当 Scroll UITableView 它与名称 iOS 重叠
【发布时间】:2014-09-04 10:39:09
【问题描述】:

当我滚动表格视图时,会出现这种类型的重叠

我在 CellForRow AtIndexPath 方法中编写以下代码,我以编程方式创建 tableview...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]];
UIImageView *view;

if (indexPath.row % 2) {
    view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dark_bg"]];
}else{
    view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"light_bg"]];
}
cell.backgroundView=view;

UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:25];
[imgView.layer setMasksToBounds:YES];
[imgView.layer setBorderColor:[UIColor whiteColor].CGColor];
[imgView setImage:[info objectForKey:@"thumbnail"]];
[cell.contentView addSubview:imgView];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)];

[label setText:[info objectForKey:@"name"]];
[cell.contentView addSubview:label];

return cell;

}

【问题讨论】:

  • 我们不是巫师或通灵者。添加一些代码,以及更多细节。
  • 您没有正确处理单元格的可重用性。在此处发布您的 cellPathForRowAtIndexPath 代码。
  • 好多了。对不起,如果我之前听起来很粗鲁。这里的问题是[cell.contentView addSubview:label];。每次调用 cellForRowAtIndexPath: 时,您每次都添加一个新标签。由于单元格被重复使用,因此之前添加的标签永远不会被删除。

标签: objective-c ios7 uicollectionview alassetslibrary alasset


【解决方案1】:

试试这个代码。你已经使用了dequeueReusableCellWithIdentifier:

NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row];

UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];

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

【讨论】:

  • 检查一下,让你知道。检查我的个人资料,你会得到我的邮件ID。
  • 请帮我将视频保存到自定义相册中
【解决方案2】:
You can try with it. Just remove the previous contentView after create a table cell.

- (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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //****add this code*******//
    for (UIImageView *v in [cell.contentView subviews])
    {
        [v removeFromSuperview];
        NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView  subviews]);
    }
    for (UILabel *v in [cell.contentView subviews])
    {
        [v removeFromSuperview];
        NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView  subviews]);
    }/////////
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *info = [ASSETHELPER getGroupInfo:_groupArray[indexPath.row]];
    UIImageView *view;
    if (indexPath.row % 2)
    {
        view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"dark_bg"]];
    }
    else
    {
        view=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"light_bg"]];
    }
    cell.backgroundView=view;
    UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
    imgView.backgroundColor=[UIColor clearColor];
    [imgView.layer setCornerRadius:25];
    [imgView.layer setMasksToBounds:YES];
    [imgView.layer setBorderColor:[UIColor whiteColor].CGColor];
    [imgView setImage:[info objectForKey:@"thumbnail"]];
    [cell.contentView addSubview:imgView];

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 30)];

    [label setText:[info objectForKey:@"name"]];
    [cell.contentView addSubview:label];

    return cell;
}

【讨论】:

  • 只需在创建单元格后添加此代码。我想这会对你有所帮助。 for (UIImageView *v in [cell.contentView subviews]) { [v removeFromSuperview]; NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView 子视图]); } for (UILabel *v in [cell.contentView subviews]) { [v removeFromSuperview]; NSLog(@"%@ hello \n\n\n\n\n%@",v,[cell.contentView 子视图]); }
  • 你知道正确格式化你的代码不是火箭科学。我已经完成了您给出的所有其他答案,但现在我只是要投反对票。一旦你正确地格式化你的答案,我会删除它,所以它是可读的 -1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 2016-03-06
相关资源
最近更新 更多