滚动时为什么UITableView单元重叠? [英] Why UITableView cell overlap while scrolling?

查看:114
本文介绍了滚动时为什么UITableView单元重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView ,大约有100行。

I have a UITableView with approx 100 row.

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

Each cell having checked image but when we scroll the UITableView, all the cells overlapped in unchecked cell.

(UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)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];

        }


推荐答案

您的代码是非常错误的...正如我所看到的,你是通过界面构建​​器创建你的单元格。在这种情况下,您为它创建自定义 UITableViewCell 类,它将 IBOutlets 添加到您的 UILabels UIImageViews (以及您想要的其他UI元素)。这将确保为您创建UI元素,并且您不必 alloc-init addSubview in你的 cellForRow 方法。

Your code is very wrong... As I can see, you're creating your cell through interface builder. In that case, you create your custom UITableViewCell class for it, which will have IBOutlets to your UILabels and UIImageViews (and other UI elements you'd like). This will make sure your UI elements get created for you and you don't have to alloc-init and addSubview in your cellForRow method.

这里需要很长时间来解释一切,所以你可以看看< a href =http://code.tutsplus.com/tutorials/ios-sdk-crafting-custom-uitableview-cells--mobile-15702\"rel =nofollow>本教程,它解释了如何创建自定义 UITableViewCells 然后我就可以了。主要是选项2:原型单元是你需要的。

It would take fairly long to explain everything here, so you could take a look at this tutorial, it explains how to create custom UITableViewCells better then I could. Mainly Option 2: Prototype Cells is what you need.

这篇关于滚动时为什么UITableView单元重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆