开发者

Load UIViews from xibs into UITableView headers, footers and cells?

开发者 https://www.devze.com 2023-04-12 13:51 出处:网络
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 UIT

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.

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:

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bou开发者_如何学Cnds.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;

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:

 cell1 = [[CellViewController alloc] initWithNibName:@"CellViewController" bundle:nil weatherData:weatherItems];

[cell1 setTitle:@"1"];

cell2 = [[CellViewController alloc] initWithNibName:@"CellViewController" bundle:nil weatherData:weatherItems];

[cell2 setTitle:@"2"];

Then inside cellForRowAtIndexPath I add cell1 and cell2 views to the current cell's view. Here's the 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;

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.

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.

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.

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.

Is this the best approach for the functionality I am trying to achieve?

Thanka!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号