• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 为什么 UITableView 单元格在滚动时重叠?

[复制链接]
菜鸟教程小白 发表于 2022-12-13 05:32:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个大约 100 行的 UITableView

每个单元格都有选中的图像,但是当我们滚动 UITableView 时,所有单元格都在未选中的单元格中重叠。

(UITableViewCell *)tableViewUITableView *)tableView
     cellForRowAtIndexPathNSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"Cell";
static NSString *MyIdentifierAds = @"Cell1";

UITableViewCell *cell ;

if (tableView==self.tblSeachMarketplace) {
    if (indexPath.section==0) {
        cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
 //            if(cell== nil)
 //            {
 //                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:MyIdentifier];
 //            }

        UILabel* lblTitle = (UILabel *)[cell viewWithTag:200];
        UILabel*lblSubheading = (UILabel *)[cell viewWithTag:300];
        UIImageView* imgRating = (UIImageView *)[cell viewWithTag:400];
        UIImageView* imgLogo = (UIImageView *)[cell viewWithTag:100];
        [lblTitle setFont:[UIFont fontWithName"Lato-Bold" size:12.5f]];
        [lblSubheading setFont:[UIFont fontWithName"Lato-Bold" size:9.0f]];
        [lblTitle setFont:[UIFont fontWithName"Lato-Bold" size:12.5f]];
        [lblSubheading setFont:[UIFont fontWithName"Lato-Bold" size:9.0f]];
        [lblTitle setTextColor:[Global colorFromHexString"#4394d4"]];
        NSDictionary*row=[[NSDictionary alloc]init];
        cell.contentView.backgroundColor =  [UIColor whiteColor];
        if (allMarketPlaceNewsletters.count>0) {
            row=[allMarketPlaceNewsletters objectAtIndex:indexPath.row];
            NSString *imgUrl=[row valueForKey"logo"];
            if (imgUrl != (id)[NSNull null]) {
                NSURL *urlTemp = [NSURL URLWithString:imgUrl];
                [imgLogo  setImageWithURL:urlTemp placeholderImage:[UIImage imageNamed"placeholder.jpg"]];
            }

            UIButton *btnLocationRight=(UIButton *)[cell viewWithTag:600];
             UIButton *btnLocationPlus=(UIButton *)[cell viewWithTag:500];

            if ([[row valueForKey"subscribed"] isEqualToString"Y"]) {
                [btnLocationRight setHidden:NO];
                [btnLocationPlus setHidden:YES];
                cell.contentView.backgroundColor =  [Global colorFromHexString"#f0f9fc"];
                [btnLocationRight setImage:[UIImage imageNamed:@"righbtn.png"] forState:UIControlStateNormal];
                 [btnLocationRight setTag:indexPath.row];
    //          [btnLocationRight addTarget:self action:@selector(ButtonPressed forControlEvents:UIControlEventTouchUpInside];
              //  [cell addSubview:[self buttonWithMap:cell :@"righbtn.png":indexPath.row]];
            }else{
                cell.contentView.backgroundColor =  [UIColor whiteColor];
                [btnLocationPlus setTag:indexPath.row];
                [btnLocationPlus addTarget:self action:@selector(ButtonPressed forControlEvents:UIControlEventTouchUpInside];

                 [btnLocationPlus setImage:[UIImage imageNamed:@"darkplusbtn.png"] forState:UIControlStateNormal];

                [btnLocationRight setHidden:YES];
                 [btnLocationPlus setHidden:NO];
               // [cell addSubview:[self buttonWithMap:cell :@"darkplusbtn.png" :indexPath.row]];
            }

            NSString *stars=[NSString stringWithFormat:@"%d.png",[[row valueForKey:@"rating"] intValue]];
            imgRating.image=[UIImage imageNamed:stars];

        }



Best Answer-推荐答案


cellForRowAtIndexPath

中添加此代码
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UIView *subview in subviews)
{
    if([subview isKindOfClass:[UIView class]])
        [subview removeFromSuperview];
    else if([subview isKindOfClass:[UIImageView class]])
        [subview removeFromSuperview];
    else if([subview isKindOfClass:[UILabel class]])
        [subview removeFromSuperview];
    else if([subview isKindOfClass:[UIImage class]])
        [subview removeFromSuperview];
    else if([subview isKindOfClass:[UIButton class]])
        [subview removeFromSuperview];

}
[subviews release];

希望这会对您有所帮助。在此先感谢您。

关于ios - 为什么 UITableView 单元格在滚动时重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118286/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap