2014-09-04 3 views
-1

Я пытаюсь создать UITableView программно, но это заставляет меня запутать, поскольку реализован метод UITableViewDataSource «с Протокол никогда не дозвонилисьUITableViewDataSource реализован метод не называется

ViewController

@interface viewController : UIViewController <UITableViewDataSource> 
@end 

#import "viewController.h" 

@interface viewController() 
@property (strong, nonatomic) NSArray *tweetsArray; 
@end 

@implementation viewController { 
    UITableView *tableView; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.tweetsArray = [[NSArray alloc] initWithObjects: 
         @"This is the first line", 
         @"This is the second line", 
         @"This is the third line", 
         @"This is the fourth line", 
         @"This is the fifth line", 
         nil]; 

    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 

    tableView.dataSource = self; 

    [self.view addSubview:tableView]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.tweetsArray count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"SettingsCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
    } 

    NSString *tweet = [self.tweetsArray objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:@"Via Cuong Doan"]; 
    [cell.detailTextLabel setText:tweet]; 

    return cell; 
}  
@end 

AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    viewController *view = [[viewController alloc] init]; 
    [self.window addSubview:view.view]; 
    return YES; 
} 

Все, кажется, прекрасно, но -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section и -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath никогда не дозвонились

+0

ViewDidLoad называется? Я считаю, что не – Injectios

+0

'viewDidLoad' вызывается, но два других не являются, поэтому меня путает –

+1

' numberOfSecion' не установлен? – Larme

ответ

2

Вам нужно создать окно в didFinishLaunchingWithOptions, а также вы не должны вручную добавить вид контроллера к окну, вы должны сделать контроллер корневого контроллера вид окна,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    ViewController *view = [[ViewController alloc] init]; 
    self.window.rootViewController = view; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
0

Пробовали ли вы:

@interface viewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

в первой строке?

+1

Это только исправило бы предупреждение компилятора и не повлияло бы на время выполнения. – rmaddy

0

Добавьте следующее:

tableView.delegate = self; 
+1

Это не поможет - рассматриваемые методы являются частью dataSource, а не делегата. – rmaddy

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