2015-06-16 2 views
0

Дело следующее: Я хочу создать UITableView из отдельного класса.UITableView из отдельного класса

В настоящее время я следующее:

// Menu.h 

@interface Menu : UITableViewController <UITableViewDelegate, UIAlertViewDelegate> {   
    UITableView *tableView; 
} 

@property (nonatomic,retain) NSMutableArray *navigationItems;  
- (void)initMenu:(UIView *)view;  
@end; 

Тогда

// Menu.m 
#import <Foundation/Foundation.h> 
#import "Menu.h" 

@implementation Menu 

- (void) initMenu:(UIView *)view { 

    self.navigationItems = [[NSMutableArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten",nil]; 

    UIView *mainmenu=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 420)]; 
    [mainmenu setBackgroundColor:[UIColor yellowColor]]; 
    [view addSubview:mainmenu]; 

    UITableView *menutableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain]; 
    menutableView.backgroundColor = [UIColor whiteColor]; 
    menutableView.delegate = self; 
    menutableView.dataSource = self; 

    [mainmenu addSubview:menutableView]; 

} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1;   
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {   
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableViewi cellForRowAtIndexPath:(NSIndexPath *)indexPath {   
    static NSString *CellIdentifier = @"Cell";   
    UITableViewCell *cell = [tableViewi dequeueReusableCellWithIdentifier:CellIdentifier];   
    if (cell == nil) {    
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text = [self.navigationItems objectAtIndex:indexPath.row]; 
    return cell; 

} 
@end 

И в другом файле .m я вызываю метод:

... 
#import "Menu.h" 
... 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    ... 
    Menu *menu = [[Menu alloc] init]; 
    [menu initMenu: self.view]; 

} 

Запуск этого приложения приведет к краху и Xcode выиграл» t дать подробный отчет. Однако, если я совмещаю Menu.m с файлом .m, где я вызываю «initMenu», он не будет разбиваться.

Также, если я прокомментирую menutableView.dataSource = self;, он будет работать с нашей катастрофой (без строк в таблице, конечно ...).

+0

Опубликовать сообщение об аварии. –

+0

Я получаю только: (lldb) – Rub4

+0

Добавить контрольную точку исключения в xcode и посмотреть, в какой строке она разбивается –

ответ

0

Передача представления в метод init, а затем создание/добавление subviews в указанном методе init является нечетным шаблоном проектирования. Измените свой класс меню на viewcontroller, а затем переместите объявления UIView и UITableView в метод viewDidLoad. Вероятно, сбой произошел из-за того, что tableview пытается отобразить данные до того, как их родительский вид завершил загрузку.