0

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

        }
    }
Davis
  • 1,253
  • 4
  • 17
  • 38

5 Answers5

3

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;
}
bhavya kothari
  • 7,484
  • 4
  • 27
  • 53
1

Please use this one:

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

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

Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29
Sandeep Agrawal
  • 425
  • 3
  • 10
1

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

        }
Samkit Jain
  • 2,523
  • 16
  • 33
  • Can you please check...if your control is going in `if(boardImg == (NSString *)[NSNull null] || boardImg == nil || boardImg.length == 0){ //If no Image exists remove the subview [newsCell.boardNoteImage removeFromSuperview]; } ` if image doen't exist / – Samkit Jain Feb 19 '14 at 13:14
  • What is `boardNoteImage` ? – Samkit Jain Feb 19 '14 at 13:19
  • Pls see my new Edit, i was a litle bit confused, sry for that! boardNoteImage is my UIIMageView which is optional! – Davis Feb 19 '14 at 13:21
  • try my edit now...if it doesn't show any image..set frames first – Samkit Jain Feb 19 '14 at 13:24
  • Now, for the first Start every Image is on the right cell, but after scrolling down and up every image is disappeared.... – Davis Feb 19 '14 at 13:38
  • Please show your code .....try allocating the imageview in first if laso...that will work – Samkit Jain Feb 19 '14 at 13:46
1

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


Shankar BS
  • 8,394
  • 6
  • 41
  • 53
  • Now i get the right images on first start, but after i scroll down the tableview and back up the images are disappeared.... – Davis Feb 19 '14 at 13:29
  • +1, Also, UITableViewCell already has UIImageView on it, so I'll recommend you use it instead your extra property. In this case will be enough to set cell.imageView.image = nil to remove imageView from cell. – Mykola Denysyuk Feb 19 '14 at 13:33
  • THAT is the Problem, after i delete the "removeFromSuperView" it works, sadly now there is always a space for the UIImageView :) – Davis Feb 19 '14 at 13:50
  • 1
    Hmm, @Shan rejected my edit, so I'll post it here: don't use `cell.boardNoteImage.frame = CGRectZero;`, better solution is to hide imageView if there no image and show back again if image exist, or even do nothing with that, because image already nilled. – Mykola Denysyuk Feb 19 '14 at 13:59
  • sorry @MykolaDenysyuk i did't notice , i went off, i will edit my answer ... :) – Shankar BS Feb 20 '14 at 04:19
0
- (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];

                 }
                 }
                 }
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42