如何将UITableView添加为SubView?

时间:2016-06-06 12:45:55

标签: ios uitableview

我需要在当前项目中添加一个UITableView作为子视图。我是Objective-C的新手,我无法找到如何以编程方式创建UITableView作为子视图的演练。这是我到目前为止所做的事情

在AppDelegate.m

MJViewController *tableViewController = [[MJViewController alloc] initWithStyle:UITableViewStyleGrouped];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:tableViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;

在MJViewController.h中

@interface MJViewController : UITableViewController

@end

在我的MJViewController.m

@interface MJViewController ()

@property (strong, nonatomic) UITableViewCell *firstNameCell;
@property (strong, nonatomic) UITableViewCell *lastNameCell;
@property (strong, nonatomic) UITableViewCell *shareCell;

@property (strong, nonatomic) UITextField *firstNameText;
@property (strong, nonatomic) UITextField *lastNameText;

@end

@implementation MJViewController

- (void)loadView
{
[super loadView];

// set the title
self.title = @"User Options";

// construct first name cell, section 0, row 0
self.firstNameCell = [[UITableViewCell alloc] init];
self.firstNameCell.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.5f];
self.firstNameText = [[UITextField alloc]initWithFrame:CGRectInset(self.firstNameCell.contentView.bounds, 15, 0)];
self.firstNameText.placeholder = @"First Name";
[self.firstNameCell addSubview:self.firstNameText];

// construct last name cell, section 0, row 1
self.lastNameCell = [[UITableViewCell alloc] init];
self.lastNameCell.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.5f];
self.lastNameText = [[UITextField alloc]initWithFrame:CGRectInset(self.lastNameCell.contentView.bounds, 15, 0)];
self.lastNameText.placeholder = @"Last Name";
[self.lastNameCell addSubview:self.lastNameText];

// construct share cell, section 1, row 00
self.shareCell = [[UITableViewCell alloc]init];
self.shareCell.textLabel.text = @"Share with Friends";
self.shareCell.backgroundColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.5f];
self.shareCell.accessoryType = UITableViewCellAccessoryCheckmark;
}



// Return the number of sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}

// Return the number of rows for each section in your static table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section)
{
    case 0:  return 2;  // section 0 has 2 rows
    case 1:  return 1;  // section 1 has 1 row
    default: return 0;
};
}

// Return the row for the corresponding section and row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch(indexPath.section)
{
    case 0:
        switch(indexPath.row)
    {
        case 0: return self.firstNameCell;  // section 0, row 0 is the first name
        case 1: return self.lastNameCell;   // section 0, row 1 is the last name
    }
    case 1:
        switch(indexPath.row)
    {
        case 0: return self.shareCell;      // section 1, row 0 is the share option
    }
}
return nil;
}



// Customize the section headings for each section
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
switch(section)
{
    case 0: return @"Profile";
    case 1: return @"Social";
}
return nil;
}


// Configure the row selection code for any cells that you want to customize the row selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Handle social cell selection to toggle checkmark
if(indexPath.section == 1 && indexPath.row == 0) {

    // deselect row
    [tableView deselectRowAtIndexPath:indexPath animated:false];

    // toggle check mark
    if(self.shareCell.accessoryType == UITableViewCellAccessoryNone)
        self.shareCell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        self.shareCell.accessoryType = UITableViewCellAccessoryNone;
}
}

@end

如何在子视图中调用MJViewController中的loidView?

1 个答案:

答案 0 :(得分:0)

您可以按照以下步骤添加表格

1)创建MJViewController类的对象

MJViewController *tableViewController = [[MJViewController alloc] initWithStyle:UITableViewStyleGrouped];

2)将tableViewController的视图添加到您想要的任何UIView对象,或者您可以将其添加到viewcontroller的视图中;

[self.view addSubview:tableViewController.view];
  

您无法直接将 tableViewController 添加为子视图   viewcontroller类型