簡體   English   中英

刪除並將子視圖添加到自定義UITableViewCell

[英]Remove and add Subview to Custom UITableViewCell

我有一個帶有不同子視圖的自定義單元格。 其中之一,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];

        }
    }
}

但這不起作用,因為現在我在每個單元格上都得到了相同的圖像……

編輯:

對不起,我忘了說我在使用Section,每個部分只有一行,這就是我使用indexPath.section的原因!

我從網絡服務器獲取所有數據,並將所有內容放入數組self.newsList!

EDIT2:使用此代碼,我可以獲得正確的單元格的正確圖像,但是向下滾動並向上備份之后,一切都消失了:/

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

        }
    }

嘗試這種方法-在情節提要中添加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;
}

請使用這個:

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

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

您正在添加圖像的部分。 因此,請執行以下操作:

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

        }

嘗試這種方式,如果情況如下所示,請不要檢查內部圖像是否存在,希望對您有所幫助,將其更改為您的要求


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


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

                 }
                 }
                 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM