滚动时为什么UITableView单元重叠?

时间:2013-11-21 10:24:46

标签: ios iphone objective-c uitableview

我有UITableView,大约有100行。

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

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

        }

2 个答案:

答案 0 :(得分:3)

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

希望这会帮助你。提前谢谢你。

答案 1 :(得分:1)

您的代码非常错误......正如我所看到的,您正在通过界面构建​​器创建您的单元格。在这种情况下,您可以为其创建自定义UITableViewCell类,其中IBOutletsUILabels(以及您想要的其他UI元素)将UIImageViews。这样可以确保为您创建UI元素,而不必使用alloc-init方法中的addSubviewcellForRow

这里需要花费很长时间来解释所有内容,因此您可以查看this tutorial,它解释了如何更好地创建自定义UITableViewCells。主要是Option 2: Prototype Cells是你需要的。