2016-06-06 3 views
-1

Мне нужно добавить UITableView в качестве подвид в моем текущем проекте. Я новичок в Objective-C, и я не могу найти пошаговое руководство о том, как создавать UITableView программно как подвью. Вот что я сделал до сих порКак добавить UITableView в качестве SubView?

В 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 

Как может Я называю loidView в MJViewController внутри subView?

+0

Где ваш метод ViewDidLoad? –

+0

Это в моем другом ViewController. И я хочу, чтобы этот UITableView загружался в этот ViewController. – drbj

+0

. Вам нужен подкласс 'MJViewController' из' UIViewController' и внутри 'loadView' инициализировать ваш tableView и добавлять в качестве подзапроса для просмотра – iSashok

ответ

0

Вы можете добавить таблицу следующие шаги

1) Создание объекта MJViewController класса

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

2) добавить вид tableViewController к любому объекту UIView вы хотите или вы можете добавить в представление ViewController к ;

[self.view addSubview:tableViewController.view]; 

Вы не можете добавлять tableViewController, как подвид непосредственно, как это ViewController типа

+0

Что я должен делать с моим AppDelegate? Причина, когда запускается программа, отображается tableViewController изначально – drbj

+0

удалите эти строки MJViewController * tableViewController = [[MJViewController alloc] initWithStyle: UITableViewStyleGrouped]; UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController: tableViewController]; , но вы должны установить диспетчер просмотра по умолчанию для своего приложения –

+0

Я получаю «элемент инициализатора не константа времени компиляции» в MJViewController * tableViewController = [[MJViewController alloc] initWithStyle: UITableViewStyleGrouped]; должен ли я включить его в реализацию моего другого ViewController? – drbj

Смежные вопросы