質問

i have a Custom Cell with different Subviews on it. One of them, a UIImageView, is optional. so there can be the case that no Image is needed in the cell. My Problem is that i dont know how i can remove the Image for cells which dont need it, and how to add the image for cell which need it? I know i only should add and remove subviews like this:

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

        }
    }
}

But that doesnt work because now i get the same Image on each cell......

EDIT:

Sry i forget to say that i´m using Section with only one row per section, thats the reason why i´m using indexPath.section!

I get all my Datas, from an webserver and put all the stuff into my array self.newsList!

EDIT2: With this Code i get the correct Images for the correct Cells, but after scrolling down and back up, everyhting is disappeared :/

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

        }
    }
役に立ちましたか?

解決

Try this approach - Add UIImageView in storyboard and you can just hide and unhide whenever you app logic required.

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

他のヒント

Please use this one:

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

You require to use indexPath.row instead of indexPath.section

You are adding the image for section. So, do as following :

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

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

        }

try by this way, dont check the presence of image inside if condition do like below hope this helps u, change it to your requirement


 - (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帰属
所属していません StackOverflow
scroll top