0

I have a UIView inside a UITableViewCell(Let's say a container).I want to add another UIView as a subView to this container.However I am able to load the subView but it doesn't show up in every cell.The reason I am doing so is because I want to load two different UIViews from nib files onto the container depending upon segment control click.So here's what I am doing.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *cellIdentifier = @"Cell";
static NSString *expandedCellIdentifier = @"ExpandedCell";
if (!isExpanded) {

    ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell==nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ListCell" owner:self options:nil];
        cell = nibs[0];
    }

    cell.Name.text = [[bArray objectAtIndex:indexPath.row]valueForKey:@"Name"];
   return cell;

}

else{


   expCell =(ExpandedCell*) [tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier]; //Made this global iVar to refrence it from other methods. 
   if (expCell==nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ExpandedCell" owner:self options:nil];
        expCell = nibs[0]; 
   lDView = [[[NSBundle mainBundle]loadNibNamed:@"lDView" owner:self options:nil]firstObject]; //lDView is a UIView subclass.
   [lDView setFrame:CGRectMake(0, 0, 225, 90)];
   [lDView setBackgroundColor:[UIColor grayColor]];
    //more code.
  }

     return expCell;
    }

    return nil;

   }

Now I add this 'lDView' to the subView after populating it with some buttons like this.

  -(void)addButtons{
    //Add buttons here
    [lDView.containerScrollView addSubview:btn]; 
   [expandedCell.containerView addSubview:lDView]; //Doesn't add it to each and every table View cell.Its loading sometimes and sometimes doesn't show up. 
iSD
  • 103
  • 10
  • These days you simply just do this with container views .. http://stackoverflow.com/a/23403979/294884. (If necessary, just have more than one in the same area and "turn on" only the desired one.) – Fattie Apr 12 '16 at 13:40

1 Answers1

0

You are doing a lot wrong here. First do not run this code:

lDView = [[[NSBundle mainBundle]loadNibNamed:@"lDView" owner:self options:nil]firstObject];
[lDView setFrame:CGRectMake(0, 0, 225, 90)];

inside cellForRowAtIndexPath method. This method gets called every time a cell is displayed (even scrolling up / down). The nib should be loaded once and the view copied / modified.

Second what you are trying to do sounds very much like the reason for prototype cells. Why not have a cell built for each case and load the correct one based on the usage, rather than loading one and adding to it every time. This is better encapsulation and easier to maintain code as then you don't have modifications everywhere in the viewController.

On a side note, if you are new to objective-c I would consider learning Storyboards + autoLayout. AutoLayout is a bit difficult to learn as its quite different to standard coding, its apples equivalent of responsive web design. Personally I think dealing with UITableView's and UITableViewCells is orders of magnitude easier in interface builder, as you can quickly whip up prototype cells and just call the correct one.

Simon McLoughlin
  • 7,803
  • 5
  • 32
  • 53
  • Even if I run the above 'loadNibNamed' in viewDidLoad, it doesn't work.Secondly, I already have two types of cell layout , one for normal height and one for expanded height, both of them from nibs.Now in expanded cell, I want to switch the above mentioned UIViews. – iSD Sep 02 '14 at 10:09
  • @iSD looking at your code again, if `lDView` is being created form a nib that contains a UITableViewCell, then calling `firstObject` is going to return the Cell, in order to add stuff to it you need to get the `contentView` of the cell and add to that, you are probably adding your views underneath the content. This is again something I would avoid in a viewController, I would create this in a nib / UItableViewCell subclass that handles this, rather than putting this in the vc. – Simon McLoughlin Sep 02 '14 at 10:15
  • 'lDView' is simply a 'UIView' subclass that I want to add to tableViewCell's container.Now since container is already in content view(I suppose.. since it is visible). I just made a 'addSubview' call on this container view. – iSD Sep 02 '14 at 10:44
  • @iSD what have you done to debug this? have you tried adding it to the screen of a viewController to test it works. Have you printing the `lDView` object to the console to make sure its being created correctly. Have you checked its not placing it outsides the bounds of the visible cell (meaning is it adding it but you just can't see it). Have you checked its no underneath something else etc. – Simon McLoughlin Sep 02 '14 at 13:19
  • Hi Simon.I made a global iVar of my custom table view cell instance to refrence it from other methods.So now I am able to add my view to container.Although it doesn't show up consistently.I updated the same question slightly since doesn't looks like a new question totally :-] – iSD Sep 02 '14 at 14:05