代码之家  ›  专栏  ›  技术社区  ›  Davis

删除子视图并将其添加到自定义UITableViewCell

  •  0
  • Davis  · 技术社区  · 11 年前

    我有一个自定义单元格,上面有不同的子视图。其中一个UIImageView是可选的。因此可能存在单元中不需要图像的情况。我的问题是,我不知道如何删除不需要的单元格的图像,以及如何为需要的单元格添加图像?我知道我只应该像这样添加和删除子视图:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier1 = @"NewsCell";
    
        GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    
        if(newsCell == nil){
    
            newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    
           //Here i get the Image from my Array (self.newsList)
            NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];
    
            //Here im checkin if an image exists
            if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
    
                //If no Image exists remove the subview
                [newsCell.boardNoteImage removeFromSuperview];
    
            } else {
    
                //If an image exists, check if the subview is already added
                if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
                    [newsCell.contentView addSubview:[newsCell.boardNoteImage];
    
            }
        }
    }
    

    但这不起作用,因为现在我在每个单元格上都得到了相同的图像。。。。。。

    编辑:

    我忘了说我使用每个节只有一行的节,这就是我使用indexPath.Section的原因!

    我从Web服务器获取所有数据,并将所有数据放入阵列self.newsList!

    编辑2: 使用此代码,我可以获得正确单元格的正确图像,但在向下滚动和向上滚动后,所有内容都消失了:/

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier1 = @"NewsCell";
    
        GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    
        if(newsCell == nil){
    
            newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    
        }
    
        NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];
    
        if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
    
            [newsCell.boardNoteImage removeFromSuperview];
            newsCell.boardNoteImage.image = nil;
        } else {
    
            if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
    
                newsCell.boardNoteImage.frame = CGRectMake(newsCell.boardNoteImage.frame.origin.x, newsCell.boardNoteImage.frame.origin.y, newsCell.boardNoteImage.frame.size.width, newsCell.boardNoteImage.frame.size.height);
    
                [newsCell.contentView addSubview:newsCell.boardNoteImage];
    
            }
        }
    
    5 回复  |  直到 11 年前
        1
  •  3
  •   bhavya kothari    11 年前

    尝试这种方法-在故事板中添加UIImageView,您可以在应用程序逻辑需要时隐藏和取消隐藏。

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SongCell"];
       if(cell == nil)
      {
         cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SongCell"];
         //dont check the presence of image in this part becz it is called during creation only not on resuing
      }
      //check if image is present or not
      NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
       if(boardImg)
       {
          //image is present
         cell.boardNoteImage.hidden = NO;
    
          cell.boardNoteImage.image = boardImg;
    
    
       }
       else
       {
         cell.boardNoteImage.frame = CGRectZero;//which places a zero rect means no image
         cell.boardNoteImage.hidden = YES;
    
       }
    
     return cell;
    }
    
        2
  •  1
  •   Bhumeshwer katre    11 年前

    请使用此选项:

    NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
    

    您需要使用 indexPath.row 而不是 indexPath.section

        3
  •  1
  •   Samkit Jain    11 年前

    您正在为节添加图像。因此,请执行以下操作:

    NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
    
      if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
           //If no Image exists remove the subview
            [newsCell.boardNoteImage removeFromSuperview];
            newsCell.boardNoteImage = nil;
    
        } else {
                //set the frames here if required
                [newsCell addSubview:boardNoteImage];
        }
    

    编辑1:

        if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
    
            [newsCell.boardNoteImage removeFromSuperview];
            newsCell.boardNoteImage.image = nil;
        } else {
    
            if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
    
                newsCell.boardNoteImage = [[UIImageView alloc] init];
    
                // SET THE IMAGE HERE
    
                newsCell.boardNoteImage.frame = CGRectMake(newsCell.boardNoteImage.frame.origin.x, newsCell.boardNoteImage.frame.origin.y, newsCell.boardNoteImage.frame.size.width, newsCell.boardNoteImage.frame.size.height);
    
                [newsCell.contentView addSubview:newsCell.boardNoteImage];
    
            }
    
        4
  •  1
  •   Shankar BS    11 年前

    通过这种方式尝试,如果条件符合以下要求,不要检查内部是否存在图像。希望这对您有所帮助,请根据您的要求进行更改


     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
       GTNewsCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SongCell"];
       if(cell == nil)
      {
         cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SongCell"];
         //dont check the presence of image in this part becz it is called during creation only not on resuing
      }
      //check if image is present or not
      NSString *boardImg = [[self.newsList objectAtIndex:indexPath.row] valueForKey:@"boardnoteImage"];
       if(boardImg)
       {
          //image is present
          cell.boardNoteImage.image = boardImg;
    
    
       }
       else
       {
         cell.boardNoteImage.frame = CGRectZero;//which places a zero rect means no image
         cell.boardNoteImage. hidden = YES;//place a nil in the image
    
       }
    
     return cell;
    }
    


        5
  •  0
  •   Bhavesh Nayi    11 年前
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        //static NSString *CellIdentifier1 = @"NewsCell";
        NSString *CellIdentifier1 = [NSString stringWithFormat:@"cell %d",indexPath.row];
    
        GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
    
        newsCell = nil;
        if(newsCell == nil)
        {
    
            newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
    
            //Here i get the Image from my Array (self.newsList)
            NSString *boardImg = [[self.newsList objectAtIndex:indexPath.section] valueForKey:@"boardnoteImage"];
    
            //Here im checkin if an image exists
            if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){
    
                //If no Image exists remove the subview
                [newsCell.boardNoteImage removeFromSuperview];
    
            } else {
    
                //If an image exists, check if the subview is already added
                if(![newsCell.boardNoteImage isDescendantOfView:newsCell.contentView ]) {
                    [newsCell.contentView addSubview:[newsCell.boardNoteImage];
    
                     }
                     }
                     }