2013-11-21 30 views
2

我有一個UITableView約100排。爲什麼在滾動時UITableView單元重疊?

每個單元格都檢查了圖像,但當我們滾動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]; 

     } 
+0

請至少提供一些代碼,'cellForRow'方法。 –

+0

@LordZsolt:我添加了代碼。 –

回答

1

你的代碼是非常錯誤的...正如我所看到的,你通過界面生成器來創建你的單元格。在這種情況下,您可以爲它創建自定義的UITableViewCell類,其中UILabelsUIImageViews(以及您想要的其他UI元素)將具有IBOutlets。這將確保您的UI元素爲您創建,並且您不必在cellForRow方法中使用alloc-initaddSubview

在這裏解釋一切需要相當長的時間,所以你可以看看this tutorial,它解釋瞭如何更好地創建自定義UITableViewCells。主要是Option 2: Prototype Cells是你需要的。

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

希望這將幫助你you.Thank提前添加該代碼。

+0

謝謝。如果它適合你,那麼請接受它,這樣可以幫助其他人。:) – Smita

+0

我的投票不算在內,因爲我是新用戶,我的聲望很低。 –

+0

好的,沒問題。:) – Smita

相關問題