2015-06-04 36 views
0

我創建了一個UIButton編程自定義UITableViewCell內的UIButtons細胞內部編號是動態的,起初它看起來完美的工作,但是當我打開所有的摺疊單元和滾動表視圖UIButton彼此重疊,如果我滾動更多的屏幕之外的視圖中的UIButton成爲隱藏,當你回到屏幕區域這裏是它的形象。UIButton的重疊和意外隱藏在裏面的UITableViewCell

我不是在這裏張貼的代碼,因爲它的大,我不知道哪一部分,我應該包括在這裏。如果被問到,我會在這裏發佈特定的代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    InterestTableViewCell *cell = (InterestTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    NSString *category= (NSString *)[self.itemsInTable objectAtIndex:indexPath.row]; 
    NSMutableArray *subcat =[[NSMutableArray alloc] init]; 
    NSArray *keys = [cat allKeys]; 
    id aKey = [keys objectAtIndex:indexPath.row]; 
    subcat = [cat objectForKey:aKey]; 
    cell.btnGroupTap.tag = indexPath.row+10000; 
    NSArray *count=(NSArray *)subcat; 
    int xoffset; 
    int yoffset = 0; 
    for(int i=0; i<count.count;i++) 
    { 
     if(i==0) 
     { 
      xoffset=0; 
     } 
     else 
     { 
      if(i%2 == 0) 
      { 
      xoffset = 0; 
      } 
      else 
      { 
       xoffset = 150; 
      } 
     } 
     if(i==0) 
     { 
      yoffset = 0; 
     } 
     else 
     { 
      if(i%2==0) 
      { 
       yoffset = yoffset+45; 
      } 
     } 
     NSString *sel = subcat[i][@"selected"]; 
     NSString *key =subcat[i][@"key"]; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     UIImage *image; 
     if(![sel boolValue]) 
     { 
      image = [UIImage imageNamed: @"unchecked.png"]; 
     } 
     else 
     { 
      image = [UIImage imageNamed: @"checked.png"]; 
     } 
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(29,18,20,20)]; 
     [imageView setImage:image]; 
     UILabel *lbl1 = [[UILabel alloc] init]; 
     [lbl1 setFrame:CGRectMake(0,5,100,20)]; 
     lbl1.backgroundColor=[UIColor clearColor]; 
     lbl1.textColor=[UIColor blackColor]; 
     lbl1.frame = CGRectMake(40,0,100,40); 
     lbl1.numberOfLines =0; 
     [lbl1 setFont:[UIFont fontWithName:@"Roboto-Light" size:12]]; 
     lbl1.text= subcat[i][@"value"]; 
     button.tag= [key integerValue]; 
     [button setTitle:@"" forState:UIControlStateNormal]; 
     button.imageView.contentMode = UIViewContentModeScaleAspectFit; 
     [button setBackgroundImage:image forState:normal]; 
     CGRect screenRect = [[UIScreen mainScreen] bounds]; 
     CGFloat screenWidth = screenRect.size.width; 
     button.frame = CGRectMake(xoffset,20+yoffset,(screenWidth/2)-40,40); 
     [button addTarget:self action:@selector(CheckAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [button addSubview:lbl1]; 
     [cell.ContainerView addSubview:button]; 
     [cell.ContainerView bringSubviewToFront:button]; 
    } 
    cell.ContainerView.hidden=YES; 
    cell.lblCategory.text = category; 
    return cell; 
} 

Full Code at github

注:didSelectRowAtIndexPath使用[tableView beginUpdates];[tableView endUpdates];

+0

請分享代碼'cellForRowAtIndexpath'方法 –

+0

請加'CheckAction:'事件代碼 –

+0

CheckAction不需要,因爲我們是無論如何也稱它,但如果你想我將分享太 –

回答

2

的事情是您使用可重複使用的細胞。把它想象成市場上的購物車。你永遠不會知道它是否是新的,也許你會得到一個已經在其中的產品。您應該始終清理您的單元格cellForRowAtIndexPath,然後繼續處理您的代碼。

#edit 似乎像清洗你的containerView應該有所幫助。像:

InterestTableViewCell *cell = (InterestTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
[[cell.ContainerView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
+0

但如何做到這一點? –

+0

由於您在單元格中的collectionView中執行所有操作,因此從每個子視圖中刪除它應該沒問題。 – sunshinejr

+0

它的工作,但不是隱藏的事,但只重疊 –

0

當您使用reusable cells通過dequeueReusableCellWithIdentifier:所以如果電池被重用這將是已經具有先前添加的CategoryView(基本上subviews)。你需要先刪除它們比屁股的新的,你需要做的是爲

for (UIView *subview in cell.ContainerView.subViews) { 
    if ([subview isKindOfClass:[UIButton class]]) { 
     [((UIButton *) subview) removeTarget:nil 
         action:NULL 
      forControlEvents:UIControlEventAllEvents]; 
     [subview removeFromSuperview]; 
    } 
} 

添加上述方法中cellForRowAtIndexPath:方法原樣

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    InterestTableViewCell *cell = (InterestTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    for (UIView *subview in cell.ContainerView.subViews) { 
    if ([subview isKindOfClass:[UIButton class]]) { 
      [((UIButton *) subview) removeTarget:nil 
             action:NULL 
      forControlEvents:UIControlEventAllEvents]; 
      [subview removeFromSuperview]; 
     } 
    } 

    NSString *category= (NSString *)[self.itemsInTable objectAtIndex:indexPath.row]; 
     NSMutableArray *subcat =[[NSMutableArray alloc] init]; 
     NSArray *keys = [cat allKeys]; 
     id aKey = [keys objectAtIndex:indexPath.row]; 
     subcat = [cat objectForKey:aKey]; 
     cell.btnGroupTap.tag = indexPath.row+10000; 
     NSArray *count=(NSArray *)subcat; 
     int xoffset; 
     int yoffset = 0; 
     for(int i=0; i<count.count;i++) 
     { 
      if(i==0) 
      { 
       xoffset=0; 
      } 
      else 
      { 
       if(i%2 == 0) 
       { 
       xoffset = 0; 
       } 
       else 
       { 
        xoffset = 150; 
       } 
      } 
      if(i==0) 
      { 
       yoffset = 0; 
      } 
      else 
      { 
       if(i%2==0) 
       { 
        yoffset = yoffset+45; 
       } 
      } 
      NSString *sel = subcat[i][@"selected"]; 
      NSString *key =subcat[i][@"key"]; 
      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
      UIImage *image; 
      if(![sel boolValue]) 
      { 
       image = [UIImage imageNamed: @"unchecked.png"]; 
      } 
      else 
      { 
       image = [UIImage imageNamed: @"checked.png"]; 
      } 
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(29,18,20,20)]; 
      [imageView setImage:image]; 
      UILabel *lbl1 = [[UILabel alloc] init]; 
      [lbl1 setFrame:CGRectMake(0,5,100,20)]; 
      lbl1.backgroundColor=[UIColor clearColor]; 
      lbl1.textColor=[UIColor blackColor]; 
      lbl1.frame = CGRectMake(40,0,100,40); 
      lbl1.numberOfLines =0; 
      [lbl1 setFont:[UIFont fontWithName:@"Roboto-Light" size:12]]; 
      lbl1.text= subcat[i][@"value"]; 
      button.tag= [key integerValue]; 
      [button setTitle:@"" forState:UIControlStateNormal]; 
      button.imageView.contentMode = UIViewContentModeScaleAspectFit; 
      [button setBackgroundImage:image forState:normal]; 
      CGRect screenRect = [[UIScreen mainScreen] bounds]; 
      CGFloat screenWidth = screenRect.size.width; 
      button.frame = CGRectMake(xoffset,20+yoffset,(screenWidth/2)-40,40); 
      [button addTarget:self action:@selector(CheckAction:) forControlEvents:UIControlEventTouchUpInside]; 
      [button addSubview:lbl1]; 
      [cell.ContainerView addSubview:button]; 
      [cell.ContainerView bringSubviewToFront:button]; 
     } 
    cell.ContainerView.hidden=YES; 
    cell.lblCategory.text = category; 
    return cell; 
} 

注意:另外,如果你是使用手動內存管理方案來管理內存比你需要創建任何subview並將其添加火release消息後,有適當的平衡你的對象的保留計數,例如。如果不再需要,最後將它從超級視圖中移除。我在您的cellForRowAtIndexPath:方法中添加了這些removeFromSuperView/release語句。

但是當你正在使用ARC比系統將管理的保留/釋放調用對象來管理內存

+0

我想你的代碼,但它不工作,不知道爲什麼 –

+0

@VarunNaharia已經更換了整個方法或者建議的更改? –

+0

建議的更改 –