Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad UIViews from xibs into UITableView headers, footers and cells?
    primarykey
    data
    text
    <p>I have a popup view that I implemented as a full size, transparent view with a smaller, UITableView on top of that. What I want to be able to do is design views inside xibs then load them into the UITableView sections like the header, footer and cells. And I want this to all be as contained as possible, using only 1 view controller and multiple xibs.</p> <p>I can do the header and footer inside one view controller easily. I made a header xib and inside IB I associated the various components to IBOutlets in my one view controller. Then in the view controller, which I implement the appropriate UITableView delegates, in viewForHeaderInSection I load the xib using:</p> <pre><code>UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease]; if (section == 0) { [headerView setBackgroundColor:[UIColor colorWithRed:0.000 green:0.002 blue:0.334 alpha:0.850]]; NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"TableHeader" owner:self options:nil]; UIView *nibView = [nibObjects objectAtIndex:0]; headerView.autoresizesSubviews = YES; [headerView addSubview:nibView]; [headerLabel1 setText:@"blah"]; [headerLabel2 setText:@"blah"]; [headerLabel3 setText:@"blah"]; [headerLabel4 setText:@"blah"]; } else { [headerView setBackgroundColor:[UIColor clearColor]]; } return headerView; </code></pre> <p>I can't get this same thing to work for the cells though. I tried the same process, grabbing the xib from nibObjects and adding the view to the cell's view, but it doesn't work. I got it to work by creating a view controller class with xib for the cells, then inside the view controller that contains my UITableView inside the viewDidLoad method I create 2 instances of that VC:</p> <pre><code> cell1 = [[CellViewController alloc] initWithNibName:@"CellViewController" bundle:nil weatherData:weatherItems]; [cell1 setTitle:@"1"]; cell2 = [[CellViewController alloc] initWithNibName:@"CellViewController" bundle:nil weatherData:weatherItems]; [cell2 setTitle:@"2"]; </code></pre> <p>Then inside cellForRowAtIndexPath I add cell1 and cell2 views to the current cell's view. Here's the code:</p> <pre><code>UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; if(indexPath.row == 0) [cell addSubview:cell1.view]; else [cell addSubview:cell2.view]; cell.userInteractionEnabled = NO; } return cell; </code></pre> <p>Then I handle setting the IBOutlet components inside the cell's view controller using self.title (that was set back in the view controller that contains the UITableView.</p> <p>This approach works but I would like to have only 1 VC class and separate xibs for the header, footer and 1 xib for the cell that can be used over and over, just need to be able to set the cell's components differently based on the row.</p> <p>I want to do this so that the header, footer and cells can be easily skinned or changed separate from the popup window as a whole.</p> <p>Also, is there a way to create multiple UIViews inside 1 xib and use them separately? That was I can also have just one xib instead of 3 for the header, footer and cell.</p> <p>Is this the best approach for the functionality I am trying to achieve?</p> <p>Thanka!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload